rokojori_action_library/RokojoriPlugin.cs

301 lines
7.1 KiB
C#
Raw Normal View History

2024-11-12 08:44:05 +00:00
using Godot;
using Rokojori;
using System.Collections.Generic;
2025-06-19 17:22:25 +00:00
using System.Globalization;
using System;
using System.Data;
2026-05-22 12:25:02 +00:00
2026-05-26 11:34:14 +00:00
#if TOOLS
2026-05-22 12:25:02 +00:00
#if ! ROKOJORI_ACTION_CORE
2026-02-13 21:28:41 +00:00
using Rokojori.Tools;
2026-05-22 12:25:02 +00:00
#endif
2024-11-12 08:44:05 +00:00
2026-05-26 11:34:14 +00:00
namespace Rokojori;
2026-05-22 12:25:02 +00:00
2026-05-26 11:34:14 +00:00
#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
2026-05-22 12:25:02 +00:00
2026-05-26 11:34:14 +00:00
public static string Path( string path )
{
return RokojoriPlugin._path + "/" + path;
}
2026-05-22 12:25:02 +00:00
2026-05-26 11:34:14 +00:00
public static string GlobalizedPath( string path )
{
return ProjectSettings.GlobalizePath( Path( path ) );
}
2026-05-22 12:25:02 +00:00
2026-05-26 11:34:14 +00:00
static readonly string RokojoriRootAutoLoad = "RokojoriRootAutoLoad";
2025-12-18 10:29:54 +00:00
2026-05-26 11:34:14 +00:00
#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
2026-05-22 12:25:02 +00:00
2026-05-26 11:34:14 +00:00
public override void _EnablePlugin()
2024-11-12 08:44:05 +00:00
{
2026-05-26 11:34:14 +00:00
AddAutoloadSingleton( RokojoriRootAutoLoad, RokojoriRootAutoLoadPath );
}
2024-11-12 08:44:05 +00:00
2026-05-26 11:34:14 +00:00
public override void _DisablePlugin()
{
RemoveAutoloadSingleton( RokojoriRootAutoLoad );
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
2025-04-23 12:00:43 +00:00
2026-05-26 11:34:14 +00:00
}
#elif ! ROKOJORI_ACTION_CORE
[Tool]
public partial class RokojoriPlugin: EditorPlugin
{
GizmoDrawerPlugin gizmoDrawerPlugin = new GizmoDrawerPlugin();
GodotEditorInspectorTools inspectorTools = new GodotEditorInspectorTools();
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
public static readonly string path = "res://addons/rokojori_action_library";
2026-05-26 11:34:14 +00:00
public static string Path( string path )
{
return RokojoriPlugin.path + "/" + path;
}
2025-10-24 11:38:51 +00:00
2026-05-26 11:34:14 +00:00
public static string GlobalizedPath( string path )
{
return ProjectSettings.GlobalizePath( Path( path ) );
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
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";
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
// static RokojoriPlugin _instance;
// public static RokojoriPlugin Get()
// {
// return _instance;
// }
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public override void _EnablePlugin()
{
this.LogInfo();
AddAutoloadSingleton( RokojoriRootAutoLoad, RokojoriRootAutoLoadPath );
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
EnsureHiddenProjectPath( RokojoriProjectInternalPath );
EnsureHiddenProjectPath( RokojoriSettingsPath );
EnsureHiddenProjectPath( RokojoriCachePath );
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
}
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
static string CreateTemporaryFilePath()
{
var directory = RokojoriCachePath + "/temp";
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
EnsureHiddenProjectPath( directory );
var id = IDGenerator.GenerateID();
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
return directory + "/" + id;
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
}
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
public static string SaveTemporaryJSON( object json )
{
var path = ProjectSettings.GlobalizePath( CreateTemporaryFilePath() );
var result = FilesSync.SaveJSON( path, json );
2025-07-03 08:34:35 +00:00
2026-05-26 11:34:14 +00:00
return path;
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public static string SaveTemporaryUTF8( string text )
{
var path = ProjectSettings.GlobalizePath( CreateTemporaryFilePath() );
var result = FilesSync.SaveUTF8( path, text );
2026-05-26 11:34:14 +00:00
return path;
}
2025-07-04 18:19:37 +00:00
2026-05-26 11:34:14 +00:00
static void EnsureHiddenProjectPath( string resPath )
{
var gdIgnorePath = resPath + "/.gdignore" ;
FilesSync.EnsureDirectoryExists( ProjectSettings.GlobalizePath( resPath ) );
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
if ( ! FilesSync.FileExists( ProjectSettings.GlobalizePath( gdIgnorePath ) ) )
{
2026-05-26 11:34:14 +00:00
FilesSync.SaveUTF8( ProjectSettings.GlobalizePath( gdIgnorePath ), "" );
}
2026-05-26 11:34:14 +00:00
}
2026-05-26 11:34:14 +00:00
static void EnsureCachedProjectPath( string cachePath )
{
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public override void _DisablePlugin()
{
this.LogInfo();
RemoveAutoloadSingleton( RokojoriRootAutoLoad );
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public override void _EnterTree()
{
2024-11-12 08:44:05 +00:00
2026-05-26 11:34:14 +00:00
EnsureHiddenProjectPath( RokojoriProjectInternalPath );
EnsureHiddenProjectPath( RokojoriSettingsPath );
EnsureHiddenProjectPath( RokojoriCachePath );
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
this.LogInfo();
2026-02-13 21:28:41 +00:00
2026-05-26 11:34:14 +00:00
AddNode3DGizmoPlugin( gizmoDrawerPlugin );
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
inspectorTools.rokojoriPlugin = this;
inspectorTools.AddPlugins();
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public override void _ExitTree()
{
this.LogInfo();
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
RemoveNode3DGizmoPlugin( gizmoDrawerPlugin );
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
inspectorTools.rokojoriPlugin = this;
inspectorTools.RemovePlugins();
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
bool wasDisposed = true;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public RokojoriPlugin()
{
wasDisposed = true;
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public class MarkerData
{
public DateTime time;
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
static readonly string markerPath = "reload-marker.json";
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
protected override void Dispose( bool disposing )
{
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
var markerData = new MarkerData();
markerData.time = DateTime.Now;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
SaveCache( markerPath, markerData );
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
float reloadDuration = 2;
DateTimeOffset lastUpdateTime = DateTime.Now;
DateTimeOffset lastDisposalTime = DateTime.Now;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public void SaveCache( string relativeCachePath, object data )
{
var path = RokojoriCachePath + "/" + relativeCachePath;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
FilesSync.SaveJSON( ProjectSettings.GlobalizePath( path ), data );
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public T LoadCache<T>( string relativeCachePath ) where T:new()
{
var path = RokojoriCachePath + "/" + relativeCachePath;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
return FilesSync.LoadJSON<T>( ProjectSettings.GlobalizePath( path ));
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public T LoadSettings<T>( string relativeSettingsPath ) where T:new()
{
var path = RokojoriSettingsPath + "/" + relativeSettingsPath;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
return FilesSync.LoadJSON<T>( ProjectSettings.GlobalizePath( path ));
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
UI editingSceneUI = null;
UIRegion editingSceneRegion = null;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
public override void _Process( double delta )
{
var editingScene = EditorInterface.Singleton.GetEditedSceneRoot();
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
if ( lastUpdateTime.HasExpired( reloadDuration ) )
{
lastUpdateTime = DateTime.Now;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
var markerData = LoadCache<MarkerData>( markerPath );
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
if ( markerData != null && markerData.time != lastDisposalTime )
{
lastDisposalTime = markerData.time;
editingScene.ForEach<IAssemblyReload>( ar => ar.OnAssemblyReloaded() );
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
}
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
if ( editingScene is UIRegion region && ! ( editingScene is UI ) )
{
if ( editingSceneRegion != region )
{
if ( editingSceneUI != null )
2025-06-19 17:22:25 +00:00
{
2026-05-26 11:34:14 +00:00
editingSceneUI.DeleteSelf();
editingSceneUI = null;
2025-06-19 17:22:25 +00:00
}
2026-05-26 11:34:14 +00:00
editingSceneRegion = region;
editingSceneUI = this.CreateChild<UI>();
editingSceneUI.settings = region.uiSettings;
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
editingSceneUI.BindChildrenOf( region );
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
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;
}
2025-06-19 17:22:25 +00:00
2026-05-26 11:34:14 +00:00
}
2024-11-12 08:44:05 +00:00
}
2026-05-26 11:34:14 +00:00
}
2025-12-18 10:29:54 +00:00
2026-05-26 11:34:14 +00:00
#endif
#endif