using Godot; using Rokojori; using System.Collections.Generic; using System.Globalization; using System; using System.Data; #if TOOLS #if ! ROKOJORI_ACTION_CORE using Rokojori.Tools; #endif namespace Rokojori; #if ROKOJORI_ACTION_CORE [Tool][RokojoriActionCoreExport] public partial class RokojoriPlugin:EditorPlugin { #if ROKOJORI_ACTION_CORE_GD public static readonly string _path = "res://addons/rokojori_action_code_gd"; #else public static readonly string _path = "res://addons/rokojori_action_code_cs"; #endif public static string Path( string path ) { return RokojoriPlugin._path + "/" + path; } public static string GlobalizedPath( string path ) { return ProjectSettings.GlobalizePath( Path( path ) ); } static readonly string RokojoriRootAutoLoad = "RokojoriRootAutoLoad"; #if ROKOJORI_ACTION_CORE_GD public static readonly string RokojoriRootAutoLoadPath = Path( "Runtime/Godot/RJ_Root.cs" ); #else public static readonly string RokojoriRootAutoLoadPath = Path( "Runtime/Godot/Root.cs" ); #endif public override void _EnablePlugin() { AddAutoloadSingleton( RokojoriRootAutoLoad, RokojoriRootAutoLoadPath ); } public override void _DisablePlugin() { RemoveAutoloadSingleton( RokojoriRootAutoLoad ); } } #elif ! ROKOJORI_ACTION_CORE [Tool] public partial class RokojoriPlugin: EditorPlugin { GizmoDrawerPlugin gizmoDrawerPlugin = new GizmoDrawerPlugin(); GodotEditorInspectorTools inspectorTools = new GodotEditorInspectorTools(); public static readonly string path = "res://addons/rokojori_action_library"; public static string Path( string path ) { return RokojoriPlugin.path + "/" + path; } public static string GlobalizedPath( string path ) { return ProjectSettings.GlobalizePath( Path( path ) ); } static readonly string RokojoriRootAutoLoad = "RokojoriRootAutoLoad"; static readonly string RokojoriRootAutoLoadPath = Path( "Runtime/Godot/Root.cs" ); static readonly string RokojoriProjectInternalPath = "res://.rokojori"; static readonly string RokojoriSettingsPath = "res://.rokojori/settings"; static readonly string RokojoriCachePath = "res://_rokojori-cache"; // static RokojoriPlugin _instance; // public static RokojoriPlugin Get() // { // return _instance; // } public override void _EnablePlugin() { this.LogInfo(); AddAutoloadSingleton( RokojoriRootAutoLoad, RokojoriRootAutoLoadPath ); EnsureHiddenProjectPath( RokojoriProjectInternalPath ); EnsureHiddenProjectPath( RokojoriSettingsPath ); EnsureHiddenProjectPath( RokojoriCachePath ); } static string CreateTemporaryFilePath() { var directory = RokojoriCachePath + "/temp"; EnsureHiddenProjectPath( directory ); var id = IDGenerator.GenerateID(); return directory + "/" + id; } public static string SaveTemporaryJSON( object json ) { var path = ProjectSettings.GlobalizePath( CreateTemporaryFilePath() ); var result = FilesSync.SaveJSON( path, json ); return path; } public static string SaveTemporaryUTF8( string text ) { var path = ProjectSettings.GlobalizePath( CreateTemporaryFilePath() ); var result = FilesSync.SaveUTF8( path, text ); return path; } static void EnsureHiddenProjectPath( string resPath ) { var gdIgnorePath = resPath + "/.gdignore" ; FilesSync.EnsureDirectoryExists( ProjectSettings.GlobalizePath( resPath ) ); if ( ! FilesSync.FileExists( ProjectSettings.GlobalizePath( gdIgnorePath ) ) ) { FilesSync.SaveUTF8( ProjectSettings.GlobalizePath( gdIgnorePath ), "" ); } } static void EnsureCachedProjectPath( string cachePath ) { } public override void _DisablePlugin() { this.LogInfo(); RemoveAutoloadSingleton( RokojoriRootAutoLoad ); } public override void _EnterTree() { EnsureHiddenProjectPath( RokojoriProjectInternalPath ); EnsureHiddenProjectPath( RokojoriSettingsPath ); EnsureHiddenProjectPath( RokojoriCachePath ); this.LogInfo(); AddNode3DGizmoPlugin( gizmoDrawerPlugin ); inspectorTools.rokojoriPlugin = this; inspectorTools.AddPlugins(); } public override void _ExitTree() { this.LogInfo(); RemoveNode3DGizmoPlugin( gizmoDrawerPlugin ); inspectorTools.rokojoriPlugin = this; inspectorTools.RemovePlugins(); } bool wasDisposed = true; public RokojoriPlugin() { wasDisposed = true; } public class MarkerData { public DateTime time; } static readonly string markerPath = "reload-marker.json"; protected override void Dispose( bool disposing ) { var markerData = new MarkerData(); markerData.time = DateTime.Now; SaveCache( markerPath, markerData ); } float reloadDuration = 2; DateTimeOffset lastUpdateTime = DateTime.Now; DateTimeOffset lastDisposalTime = DateTime.Now; public void SaveCache( string relativeCachePath, object data ) { var path = RokojoriCachePath + "/" + relativeCachePath; FilesSync.SaveJSON( ProjectSettings.GlobalizePath( path ), data ); } public T LoadCache( string relativeCachePath ) where T:new() { var path = RokojoriCachePath + "/" + relativeCachePath; return FilesSync.LoadJSON( ProjectSettings.GlobalizePath( path )); } public T LoadSettings( string relativeSettingsPath ) where T:new() { var path = RokojoriSettingsPath + "/" + relativeSettingsPath; return FilesSync.LoadJSON( ProjectSettings.GlobalizePath( path )); } UI editingSceneUI = null; UIRegion editingSceneRegion = null; public override void _Process( double delta ) { var editingScene = EditorInterface.Singleton.GetEditedSceneRoot(); if ( lastUpdateTime.HasExpired( reloadDuration ) ) { lastUpdateTime = DateTime.Now; var markerData = LoadCache( markerPath ); if ( markerData != null && markerData.time != lastDisposalTime ) { lastDisposalTime = markerData.time; editingScene.ForEach( ar => ar.OnAssemblyReloaded() ); } } if ( editingScene is UIRegion region && ! ( editingScene is UI ) ) { if ( editingSceneRegion != region ) { if ( editingSceneUI != null ) { editingSceneUI.DeleteSelf(); editingSceneUI = null; } editingSceneRegion = region; editingSceneUI = this.CreateChild(); editingSceneUI.settings = region.uiSettings; editingSceneUI.BindChildrenOf( region ); } editingSceneUI.updateMode = UI.UpdateMode.Only_Manual_Updates; if ( region.reassignUI ) { editingSceneUI.settings = region.uiSettings; region.SetUI( editingSceneUI ); editingSceneUI.BindChildrenOf( region ); region.reassignUI = false; } if ( region.updateInEditor ) { region.SetUI( editingSceneUI ); editingSceneUI.BindChildrenOf( region ); editingSceneUI.fontZoom = region.fontZoom; editingSceneUI.UpdateExternal( (float)delta ); region.Layout(); region.computedFontSize = editingSceneUI.X_computedFontSizePixels; } } } } #endif #endif