Files
Convention-Unity-Demo/Assets/Scripts/Framework/ScriptableObjectInstantiate/DefaultScriptableObjectInstantiate.cs

345 lines
16 KiB
C#

using Convention;
using Convention.RScript;
using Convention.WindowsUI.Variant;
using Demo.Game;
using Demo.PrivateType;
using Dreamteck.Splines;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace Demo
{
namespace Game
{
public static class DefaultInstantiate
{
public static Dictionary<string, Func<ScriptableObject>> GameObjectInstantiate = new()
{
{ $"{nameof(Anchor)}",Anchor.Make},
// Camera
{ $"{nameof(CameraObject)}",CameraObject.MakeCameraObject},
// Global Env
{ $"{nameof(PrefabRootObject)}",PrefabRootObject.Make },
{ $"{nameof(SkyUpdatement)}",SkyUpdatement.Make},
// Sub World
{ $"{nameof(SubWorld)}",SubWorld.Make},
};
public static Dictionary<string, Func<ScriptableObject>> DDTInstantiate = new()
{
{ $"{nameof(DDT)}",DDT.Make}
};
public static Dictionary<string, Func<ScriptableObject>> TickUpdatementInstantiate = new()
{
{ $"{nameof(LookAtAnchor)}",LookAtAnchor.Make},
{ $"{nameof(TickMovement)}",TickMovement.Make},
{ $"{nameof(TickRotation)}",TickRotation.Make},
{ $"{nameof(TickScaling)}",TickScaling.Make},
};
public static Dictionary<string, Func<ScriptableObject>> MaterialUpdatementInstantiate = new()
{
{ $"{nameof(MaterialUpdatement)}",MaterialUpdatement.Make},
{ $"{nameof(ColorUpdatement)}",ColorUpdatement.Make},
{ $"{nameof(EmissionColorUpdatement)}",EmissionColorUpdatement.Make},
};
public static Dictionary<string, Func<ScriptableObject>> SplineInstantiate = new()
{
{ $"{nameof(SplineCore)}",SplineCore.Make},
{ $"{nameof(SplineNode)}",SplineNode.Make},
{ $"{nameof(SplineAnchor)}",SplineAnchor.Make},
{ $"{nameof(SplineMovement)}",SplineMovement.Make},
{ $"{nameof(SplineRotation)}",SplineRotation.Make},
{ $"{nameof(SplineScaling)}",SplineScaling.Make},
{ $"{nameof(SplinePointerObject)}",SplinePointerObject.Make},
{ $"{nameof(SplineTrackRenderer)}",SplineTrackRenderer.Make},
{ $"{nameof(SplineTubeRenderer)}",SplineTubeRenderer.Make},
{ $"{nameof(SplineSurfaceRenderer)}",SplineSurfaceRenderer.Make},
{ $"{nameof(SplineRenderer)}",SplineRenderer.Make},
};
public static Dictionary<string, Func<ScriptableObject>> JudgementInstantiate = new()
{
// 可判定物只能有一个种类被使用,否则会引起输入检定冲突
{ $"{nameof(FullScreenInteraction)}",FullScreenInteraction.Make},
// Effect
{ $"{nameof(ParticleEffect)}",ParticleEffect.Make },
// JudgementEffect
{ $"{nameof(ParticleJudgement)}",ParticleJudgement.Make},
};
public static void OpenInstantiateMenu(this ScriptableObject self, RectTransform item)
{
List<SharedModule.CallbackData> result = new()
{
// Show Name
new($"{self.ScriptName}",_=>{ }),
new($"{self.GetType()}",_=>{ }),
new($"Pos:{self.transform.position}",_=>{ }),
new($"xPos:{self.transform.localPosition}",_=>{ }),
new($"Angles:{self.transform.eulerAngles}",_=>{ }),
new($"xAngles:{self.transform.localEulerAngles}",_=>{ }),
new($"Scale:{self.transform.localScale}",_=>{ }),
};
SharedModule.instance.OpenCustomMenu(item, result.ToArray());
}
public static Dictionary<string, Func<ScriptableObject>> GetScriptableObjectInstantiate()
{
return new Dictionary<string, Func<ScriptableObject>>(GameObjectInstantiate
.Union(DDTInstantiate)
.Union(TickUpdatementInstantiate)
.Union(MaterialUpdatementInstantiate)
.Union(SplineInstantiate)
.Union(JudgementInstantiate));
}
}
}
namespace PrivateType
{
public class EaseCurveTypeInstance
{
public static EaseCurveTypeInstance instance = new();
public MathExtension.EaseCurveType Linear => MathExtension.EaseCurveType.Linear;
public MathExtension.EaseCurveType InQuad => MathExtension.EaseCurveType.InQuad;
public MathExtension.EaseCurveType OutQuad => MathExtension.EaseCurveType.OutQuad;
public MathExtension.EaseCurveType InOutQuad => MathExtension.EaseCurveType.InOutQuad;
public MathExtension.EaseCurveType InCubic => MathExtension.EaseCurveType.InCubic;
public MathExtension.EaseCurveType OutCubic => MathExtension.EaseCurveType.OutCubic;
public MathExtension.EaseCurveType InOutCubic => MathExtension.EaseCurveType.InOutCubic;
public MathExtension.EaseCurveType InQuart => MathExtension.EaseCurveType.InQuart;
public MathExtension.EaseCurveType OutQuart => MathExtension.EaseCurveType.OutQuart;
public MathExtension.EaseCurveType InOutQuart => MathExtension.EaseCurveType.InOutQuart;
public MathExtension.EaseCurveType InQuint => MathExtension.EaseCurveType.InQuint;
public MathExtension.EaseCurveType OutQuint => MathExtension.EaseCurveType.OutQuint;
public MathExtension.EaseCurveType InOutQuint => MathExtension.EaseCurveType.InOutQuint;
public MathExtension.EaseCurveType InSine => MathExtension.EaseCurveType.InSine;
public MathExtension.EaseCurveType OutSine => MathExtension.EaseCurveType.OutSine;
public MathExtension.EaseCurveType InOutSine => MathExtension.EaseCurveType.InOutSine;
public MathExtension.EaseCurveType InExpo => MathExtension.EaseCurveType.InExpo;
public MathExtension.EaseCurveType OutExpo => MathExtension.EaseCurveType.OutExpo;
public MathExtension.EaseCurveType InOutExpo => MathExtension.EaseCurveType.InOutExpo;
public MathExtension.EaseCurveType InCirc => MathExtension.EaseCurveType.InCirc;
public MathExtension.EaseCurveType OutCirc => MathExtension.EaseCurveType.OutCirc;
public MathExtension.EaseCurveType InOutCirc => MathExtension.EaseCurveType.InOutCirc;
public MathExtension.EaseCurveType InBounce => MathExtension.EaseCurveType.InBounce;
public MathExtension.EaseCurveType OutBounce => MathExtension.EaseCurveType.OutBounce;
public MathExtension.EaseCurveType InOutBounce => MathExtension.EaseCurveType.InOutBounce;
public MathExtension.EaseCurveType InElastic => MathExtension.EaseCurveType.InElastic;
public MathExtension.EaseCurveType OutElastic => MathExtension.EaseCurveType.OutElastic;
public MathExtension.EaseCurveType InOutElastic => MathExtension.EaseCurveType.InOutElastic;
public MathExtension.EaseCurveType InBack => MathExtension.EaseCurveType.InBack;
public MathExtension.EaseCurveType OutBack => MathExtension.EaseCurveType.OutBack;
public MathExtension.EaseCurveType InOutBack => MathExtension.EaseCurveType.InOutBack;
public MathExtension.EaseCurveType Custom => MathExtension.EaseCurveType.Custom;
}
public class SplineComputerSampleModeInstance
{
public static SplineComputerSampleModeInstance instance = new();
public SplineComputer.SampleMode Default => SplineComputer.SampleMode.Default;
public SplineComputer.SampleMode Uniform => SplineComputer.SampleMode.Uniform;
public SplineComputer.SampleMode Optimized => SplineComputer.SampleMode.Optimized;
}
public class SplineTypeInstance
{
public static SplineTypeInstance instance = new();
public Spline.Type Linear => Spline.Type.Linear;
public Spline.Type BSpline => Spline.Type.BSpline;
public Spline.Type CatmullRom => Spline.Type.CatmullRom;
public Spline.Type Bezier => Spline.Type.Bezier;
}
public class IEffectHookObjectInstance
{
public static IEffectHookObjectInstance instance = new();
public IEffectHookObject.InteractiveEffectType VisibleDuration => IEffectHookObject.InteractiveEffectType.VisibleDuration;
public IEffectHookObject.InteractiveEffectType InteractiveDuration => IEffectHookObject.InteractiveEffectType.InteractiveDuration;
public IEffectHookObject.InteractiveEffectType InteractableScoreInterval => IEffectHookObject.InteractiveEffectType.InteractableScoreInterval;
public IEffectHookObject.InteractiveEffectType InteractableIntervalThatCanScoreBest => IEffectHookObject.InteractiveEffectType.InteractableIntervalThatCanScoreBest;
}
public class IInteractionJudgementLevelInstance
{
public static IInteractionJudgementLevelInstance instance = new();
public IInteraction.JudgementLevel Bad => IInteraction.JudgementLevel.Bad;
public IInteraction.JudgementLevel ScoreLevel => IInteraction.JudgementLevel.ScoreLevel;
public IInteraction.JudgementLevel None => IInteraction.JudgementLevel.None;
public IInteraction.JudgementLevel BestLevel => IInteraction.JudgementLevel.BestLevel;
}
public static class RandomTool
{
public static float Random(float min, float max)
{
return UnityEngine.Random.Range(min, max);
}
public static float Random(double min, double max)
{
return UnityEngine.Random.Range((float)min, (float)max);
}
}
public class ConsoleTool
{
private GameObject gameObject;
public ConsoleTool(GameObject gameObject)
{
this.gameObject = gameObject;
}
public void Log(object obj)
{
Debug.Log(obj, gameObject);
}
}
}
public partial class ScriptableObject
{
protected virtual bool IsImptSerialize => false;
protected virtual IEnumerator LoadFromImptCacheFile(ToolFile cacheFile)
{
throw new NotImplementedException();
}
protected virtual IEnumerator CreateAndLoadingImptCacheFile(ToolFile scriptFile, ToolFile cacheFile)
{
throw new NotImplementedException();
}
private readonly RScriptImportClass s_GenerateImport = new()
{
typeof(Mathf),
typeof(RandomTool),
};
public RScriptImportClass GenerateImport()
{
return s_GenerateImport;
}
public RScriptVariables GenerateVariables(ScriptableObject self)
{
RScriptVariables variables = new()
{
{ "this", new() { data = self, type = self.GetType() } },
{ "self", new() { data = self, type = self.GetType() } },
{ "console", new() { data = new ConsoleTool(gameObject), type = typeof(ConsoleTool) } },
{ nameof(MathExtension.EaseCurveType), new() { data = EaseCurveTypeInstance.instance, type = typeof(EaseCurveTypeInstance) } },
{ $"Spline{nameof(SplineComputer.SampleMode)}",
new() { data = SplineComputerSampleModeInstance.instance, type = typeof(SplineComputerSampleModeInstance)} },
{ $"Spline{nameof(Spline.Type)}",
new() { data = SplineTypeInstance.instance, type = typeof(SplineTypeInstance)} },
{ nameof(IEffectHookObject.InteractiveEffectType),
new() { data = IEffectHookObjectInstance.instance, type = typeof(IEffectHookObjectInstance)} },
{ nameof(IInteraction.JudgementLevel),
new() { data = IInteractionJudgementLevelInstance.instance, type = typeof(IInteractionJudgementLevelInstance) } }
};
return variables;
}
private static readonly Dictionary<string, object> s_FileLocker = new();
public IEnumerator ParseFromScriptFile2Expr(ToolFile file)
{
IsParseScript2Expr = true;
try
{
var hash = file.CalculateHash();
var lastHashFile = file.GetParentDir() | ".cache" | $"{file.GetFilename(true)}.hash";
var bin = file.GetParentDir() | ".cache" | $"{file.GetFilename(true)}.bin";
if (IsImptSerialize)
{
if (lastHashFile.Exists() == false || lastHashFile.LoadAsText() != hash)
{
lastHashFile.MustExistsPath();
lastHashFile.SaveAsText(hash);
yield return ConventionUtility.AvoidFakeStop(CreateAndLoadingImptCacheFile(file, bin));
}
else
{
yield return ConventionUtility.AvoidFakeStop(LoadFromImptCacheFile(bin));
}
}
else
{
IEnumerator step = null;
RScriptEngine engine = new();
RScriptImportClass importClass = GenerateImport();
RScriptVariables variables = GenerateVariables(this);
object locker;
if (lastHashFile.Exists() == false || lastHashFile.LoadAsText() != hash)
{
lastHashFile.MustExistsPath();
bin.MustExistsPath();
lastHashFile.SaveAsText(hash);
var script = file.LoadAsText();
var structBin = engine.Compile(script, importClass, variables);
lock (s_FileLocker)
{
if (s_FileLocker.TryGetValue(file.GetFullPath(), out locker) == false)
{
s_FileLocker.Add(file.GetFullPath(), locker = new object());
}
}
lock (locker)
{
bin.SaveAsBinary(RScriptSerializer.SerializeClass(structBin));
}
step = engine.RunAsync(script, importClass, variables);
}
else
{
RScriptContext.SerializableClass structBin;
lock (s_FileLocker)
{
if (s_FileLocker.TryGetValue(file.GetFullPath(), out locker) == false)
{
s_FileLocker.Add(file.GetFullPath(), locker = new object());
}
}
lock (locker)
{
structBin = RScriptSerializer.DeserializeClass(bin.LoadAsBinary());
}
step = engine.RunAsync(structBin, importClass, variables);
}
yield return step;// ConventionUtility.AvoidFakeStop(step);
}
}
finally
{
IsParseScript2Expr = false;
}
}
public IEnumerator ParseScript2Expr(string script)
{
IsParseScript2Expr = true;
try
{
RScriptEngine engine = new();
RScriptImportClass importClass = GenerateImport();
RScriptVariables variables = GenerateVariables(this);
var step = engine.RunAsync(script, importClass, variables);
yield return step;//ConventionUtility.AvoidFakeStop(step);
}
finally
{
IsParseScript2Expr = false;
}
}
}
}