replace minigame loader reflection with source generator

This commit is contained in:
minenice55 2024-01-18 19:56:26 -05:00
parent c341715901
commit ed78a4ba2f
9 changed files with 891 additions and 33 deletions

View file

@ -0,0 +1,124 @@
using static SatorImaging.UnitySourceGenerator.USGFullNameOf;
using SatorImaging.UnitySourceGenerator;
using System.Text;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using HeavenStudio;
// HOW TO USE: Add the following attribute to *target* class.
// Note that target class must be defined as partial.
//[UnitySourceGenerator(typeof(MinigameLoaderGenerator), OverwriteIfFileExists = false)]
public partial class MinigameLoaderGenerator
{
#if UNITY_EDITOR // USG: class definition is required to avoid build error but methods are not.
#pragma warning disable IDE0051
readonly static string MEMBER_ACCESS = "public static";
readonly static string MAIN_MEMBER_NAME = "LoadMinigames";
static string OutputFileName() => MAIN_MEMBER_NAME + ".cs"; // -> Name.<TargetClass>.<GeneratorClass>.g.cs
static bool Emit(USGContext context, StringBuilder sb)
{
List<Func<EventCaller, Minigames.Minigame>> loadRunners = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(x => x.Namespace == "HeavenStudio.Games.Loaders" && x.GetMethod("AddGame", BindingFlags.Public | BindingFlags.Static) != null)
.Select(t => (Func<EventCaller, Minigames.Minigame>)Delegate.CreateDelegate(
typeof(Func<EventCaller, Minigames.Minigame>),
null,
t.GetMethod("AddGame", BindingFlags.Public | BindingFlags.Static),
false
))
.ToList();
// USG: static classes are IsAbstract is set.
if (!context.TargetClass.IsClass)
return false; // return false to tell USG doesn't write file.
// USG: you can modify output path. default file name is that USG generated.
// note that USG doesn't care the modified path is valid or not.
//context.OutputPath += "_MyFirstTest.txt";
// USG: EditorUtility.DisplayDialog() or others don't work in batch mode.
// throw if method depending on GUI based functions.
//if (UnityEngine.Application.isBatchMode)
// throw new System.NotSupportedException("GUI based functions do nothing in batch mode.");
// USG: write content into passed StringBuilder.
sb.Append($@"
using System;
using System.Collections.Generic;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
using HeavenStudio;
using HeavenStudio.Games.Loaders;
namespace {context.TargetClass.Namespace}
{{
static partial class {context.TargetClass.Name}
{{
");
// class open ----------------------------------------------------------------------
#region // USG: MainMember
sb.Append($@"
{MEMBER_ACCESS} void {MAIN_MEMBER_NAME}(EventCaller eventCaller)
{{
");
sb.IndentLevel(3);
sb.Append($@"
Minigames.Minigame game;
");
foreach (var loadRunner in loadRunners)
{
MethodInfo methodInfo = RuntimeReflectionExtensions.GetMethodInfo(loadRunner);
string callingClass = methodInfo.DeclaringType.Name;
string method = methodInfo.Name;
string fullMethodLabel = $"{callingClass}.{method}";
sb.Append($@"
Debug.Log(""Running game loader {callingClass}"");
game = {fullMethodLabel}(eventCaller);
if (game != null)
{{
eventCaller.minigames.Add(game.name, game);
}}
else
{{
Debug.LogWarning(""Game loader {callingClass} failed!"");
}}
");
}
// USG: semicolon?
sb.Append($@"
}}
");
#endregion
// class close ----------------------------------------------------------------------
sb.Append($@"
}}
}}
");
// USG: return true to tell USG to write content into OutputPath. false to do nothing.
return true;
}
#pragma warning restore IDE0051
#endif
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0ca52f3b3768be34293826705b5cdf6c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -12,6 +12,8 @@ using HeavenStudio.Editor.Track;
using HeavenStudio.Games;
using Jukebox;
using SatorImaging.UnitySourceGenerator;
using System;
using System.Linq;
using System.Reflection;
@ -19,8 +21,8 @@ using System.IO;
namespace HeavenStudio
{
public class Minigames
[UnitySourceGenerator(typeof(MinigameLoaderGenerator), OverwriteIfFileExists = false)]
public partial class Minigames
{
public enum RecommendedControlStyle
{
@ -712,24 +714,6 @@ namespace HeavenStudio
public delegate void EventCallback();
public delegate void ParamChangeCallback(string paramName, object paramValue, RiqEntity entity);
// overengineered af but it's a modified version of
// https://stackoverflow.com/a/19877141
static List<Func<EventCaller, Minigame>> loadRunners;
static void BuildLoadRunnerList()
{
loadRunners = System.Reflection.Assembly.GetExecutingAssembly()
.GetTypes()
.Where(x => x.Namespace == "HeavenStudio.Games.Loaders" && x.GetMethod("AddGame", BindingFlags.Public | BindingFlags.Static) != null)
.Select(t => (Func<EventCaller, Minigame>)Delegate.CreateDelegate(
typeof(Func<EventCaller, Minigame>),
null,
t.GetMethod("AddGame", BindingFlags.Public | BindingFlags.Static),
false
))
.ToList();
}
public static void Init(EventCaller eventCaller)
{
List<Minigame> defaultGames = new()
@ -1188,19 +1172,7 @@ namespace HeavenStudio
eventCaller.minigames.Add(game.name, game);
}
BuildLoadRunnerList();
Debug.Log($"Running {loadRunners.Count} game loaders...");
foreach (var load in loadRunners)
{
Debug.Log("Running game loader " + RuntimeReflectionExtensions.GetMethodInfo(load).DeclaringType.Name);
Minigame game = load(eventCaller);
if (game == null)
{
Debug.LogError("Game loader " + RuntimeReflectionExtensions.GetMethodInfo(load).DeclaringType.Name + " failed!");
continue;
}
eventCaller.minigames.Add(game.name, game);
}
LoadMinigames(eventCaller);
}
}
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 27b35f83276a4ce4a8e82bc332f19374
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,701 @@
// <auto-generated>MinigameLoaderGenerator</auto-generated>
using System;
using System.Collections.Generic;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
using HeavenStudio;
using HeavenStudio.Games.Loaders;
namespace HeavenStudio
{
static partial class Minigames
{
public static void LoadMinigames(EventCaller eventCaller)
{
Minigames.Minigame game;
Debug.Log("Running game loader RvlBadmintonLoader");
game = RvlBadmintonLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlBadmintonLoader failed!");
}
Debug.Log("Running game loader CtrBearLoader");
game = CtrBearLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader CtrBearLoader failed!");
}
Debug.Log("Running game loader RvlBoardMeetingLoader");
game = RvlBoardMeetingLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlBoardMeetingLoader failed!");
}
Debug.Log("Running game loader NtrFlickLoader");
game = NtrFlickLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrFlickLoader failed!");
}
Debug.Log("Running game loader CtrCatchLoader");
game = CtrCatchLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader CtrCatchLoader failed!");
}
Debug.Log("Running game loader RvlBookLoader");
game = RvlBookLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlBookLoader failed!");
}
Debug.Log("Running game loader AgbClapLoader");
game = AgbClapLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbClapLoader failed!");
}
Debug.Log("Running game loader NtrCoinLoader");
game = NtrCoinLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrCoinLoader failed!");
}
Debug.Log("Running game loader NtrCropLoader");
game = NtrCropLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrCropLoader failed!");
}
Debug.Log("Running game loader NtrDjLoader");
game = NtrDjLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrDjLoader failed!");
}
Debug.Log("Running game loader NtrDogNinjaLoader");
game = NtrDogNinjaLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrDogNinjaLoader failed!");
}
Debug.Log("Running game loader RvlDoubleDateLoader");
game = RvlDoubleDateLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlDoubleDateLoader failed!");
}
Debug.Log("Running game loader CtrDrummingLoader");
game = CtrDrummingLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader CtrDrummingLoader failed!");
}
Debug.Log("Running game loader NtrIdolLoader");
game = NtrIdolLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrIdolLoader failed!");
}
Debug.Log("Running game loader AgbFireworkLoader");
game = AgbFireworkLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbFireworkLoader failed!");
}
Debug.Log("Running game loader CtrFirstContact");
game = CtrFirstContact.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader CtrFirstContact failed!");
}
Debug.Log("Running game loader RvlFlipperFlopLoader");
game = RvlFlipperFlopLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlFlipperFlopLoader failed!");
}
Debug.Log("Running game loader RvlForkLoader");
game = RvlForkLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlForkLoader failed!");
}
Debug.Log("Running game loader NtrGleeClubLoader");
game = NtrGleeClubLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrGleeClubLoader failed!");
}
Debug.Log("Running game loader RvlNewKarateLoader");
game = RvlNewKarateLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlNewKarateLoader failed!");
}
Debug.Log("Running game loader CtrTeppanLoader");
game = CtrTeppanLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader CtrTeppanLoader failed!");
}
Debug.Log("Running game loader RvlRocketLoader");
game = RvlRocketLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlRocketLoader failed!");
}
Debug.Log("Running game loader NtrBackbeatLoader");
game = NtrBackbeatLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrBackbeatLoader failed!");
}
Debug.Log("Running game loader AgbMarcherLoader");
game = AgbMarcherLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbMarcherLoader failed!");
}
Debug.Log("Running game loader PcoMeatLoader");
game = PcoMeatLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader PcoMeatLoader failed!");
}
Debug.Log("Running game loader AgbUpbeatLoader");
game = AgbUpbeatLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbUpbeatLoader failed!");
}
Debug.Log("Running game loader ntrMunchyMonkLoader");
game = ntrMunchyMonkLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader ntrMunchyMonkLoader failed!");
}
Debug.Log("Running game loader NtrOctopusMachineLoader");
game = NtrOctopusMachineLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrOctopusMachineLoader failed!");
}
Debug.Log("Running game loader CtrPillowLoader");
game = CtrPillowLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader CtrPillowLoader failed!");
}
Debug.Log("Running game loader AgbQuizShowLoader");
game = AgbQuizShowLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbQuizShowLoader failed!");
}
Debug.Log("Running game loader NtrPingpongLoader");
game = NtrPingpongLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrPingpongLoader failed!");
}
Debug.Log("Running game loader PcoSomenLoader");
game = PcoSomenLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader PcoSomenLoader failed!");
}
Debug.Log("Running game loader AgbHairLoader");
game = AgbHairLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbHairLoader failed!");
}
Debug.Log("Running game loader RvlRingsideLoader");
game = RvlRingsideLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlRingsideLoader failed!");
}
Debug.Log("Running game loader NtrRockersLoader");
game = NtrRockersLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrRockersLoader failed!");
}
Debug.Log("Running game loader NtrSamuraiLoader");
game = NtrSamuraiLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrSamuraiLoader failed!");
}
Debug.Log("Running game loader RvlSeeSawLoader");
game = RvlSeeSawLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlSeeSawLoader failed!");
}
Debug.Log("Running game loader AgbGhostLoader");
game = AgbGhostLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbGhostLoader failed!");
}
Debug.Log("Running game loader AgbBatterLoader");
game = AgbBatterLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbBatterLoader failed!");
}
Debug.Log("Running game loader AgbSpaceDanceLoader");
game = AgbSpaceDanceLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbSpaceDanceLoader failed!");
}
Debug.Log("Running game loader NtrSoccerLoader");
game = NtrSoccerLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrSoccerLoader failed!");
}
Debug.Log("Running game loader NtrSplashdownLoader");
game = NtrSplashdownLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrSplashdownLoader failed!");
}
Debug.Log("Running game loader RvlTambourineLoader");
game = RvlTambourineLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlTambourineLoader failed!");
}
Debug.Log("Running game loader AgbTapLoader");
game = AgbTapLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbTapLoader failed!");
}
Debug.Log("Running game loader RvlTapTroupeLoader");
game = RvlTapTroupeLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlTapTroupeLoader failed!");
}
Debug.Log("Running game loader NtrDazzlesLoader");
game = NtrDazzlesLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrDazzlesLoader failed!");
}
Debug.Log("Running game loader AgbTossBoysLoader");
game = AgbTossBoysLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbTossBoysLoader failed!");
}
Debug.Log("Running game loader AgbTramLoader");
game = AgbTramLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbTramLoader failed!");
}
Debug.Log("Running game loader MobTrickLoader");
game = MobTrickLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader MobTrickLoader failed!");
}
Debug.Log("Running game loader NtrTunnelLoader");
game = NtrTunnelLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader NtrTunnelLoader failed!");
}
Debug.Log("Running game loader AgbWaltzLoader");
game = AgbWaltzLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader AgbWaltzLoader failed!");
}
Debug.Log("Running game loader RvlWorkingDoughLoader");
game = RvlWorkingDoughLoader.AddGame(eventCaller);
if (game != null)
{
eventCaller.minigames.Add(game.name, game);
}
else
{
Debug.LogWarning("Game loader RvlWorkingDoughLoader failed!");
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 859d77d7026ac1e4288a5c844917fc22
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -4,6 +4,7 @@
"com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask",
"com.nobi.roundedcorners": "https://github.com/kirevdokimov/Unity-UI-Rounded-Corners.git",
"com.rheavenstudio.jukebox": "https://github.com/RHeavenStudio/Jukebox.git?path=/Assets/Scripts/Jukebox",
"com.sator-imaging.alt-source-generator": "https://github.com/sator-imaging/Unity-AltSourceGenerator.git",
"com.sr4dev.unity-spriteassist": "https://github.com/sr4dev/Unity-SpriteAssist.git?path=Assets/SpriteAssist",
"com.tayx.graphy": "https://github.com/Tayx94/graphy.git",
"com.unity.2d.sprite": "1.0.0",

View file

@ -31,6 +31,15 @@
},
"hash": "03264e671d2c1761f3e5ce57c982e75c6d556a60"
},
"com.sator-imaging.alt-source-generator": {
"version": "https://github.com/sator-imaging/Unity-AltSourceGenerator.git",
"depth": 0,
"source": "git",
"dependencies": {
"com.unity.modules.uielements": "1.0.0"
},
"hash": "4fcd4088cda76e117e76780cea7d85c484a69d94"
},
"com.sr4dev.unity-spriteassist": {
"version": "https://github.com/sr4dev/Unity-SpriteAssist.git?path=Assets/SpriteAssist",
"depth": 0,

View file

@ -0,0 +1,21 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &1
MonoBehaviour:
m_ObjectHideFlags: 53
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c40623a0469766b4095282517245602d, type: 3}
m_Name:
m_EditorClassIdentifier:
AutoEmitOnScriptUpdate: 1
AutoEmitDisabledPaths: []
DenseViewWidthThreshold: 512
_disableAutoReloadInBackground: 0
ImportedScriptPaths: []
PathsToSkipImportEvent: []
PathsToIgnoreOverwriteSettingOnAttribute: []