Skip to content

Commit

Permalink
Load directly into SGB Save (#28)
Browse files Browse the repository at this point in the history
This PR modifies the LoadSmileGameAsync function so that you can optionally chose a save file to DIRECTLY load into. This entirely skips the title screen.
  • Loading branch information
Iseeicy authored Aug 15, 2023
1 parent 210325b commit 48d9c34
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 17 deletions.
64 changes: 58 additions & 6 deletions Runtime/Scripts/SGBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static SGBManager()


// Load into a Smile Game Builder game, given the location of it's assets
public static async Task LoadSmileGameAsync(string smileGameName)
public static async Task LoadSmileGameAsync(string smileGameName, int saveFile = -1)
{
// Check to see if there's a smile game by this name
if (!DoesSGBSubpathExist(smileGameName))
Expand All @@ -50,12 +50,27 @@ public static async Task LoadSmileGameAsync(string smileGameName)
// Set the game subpath we want to load
UnityEntry.gameSubpathName = smileGameName;

// Override natural init if we are given a save to directly load
UnityEntry.overrideInit = saveFile != -1;

// Start async loading the scene
await EnterWorkspaceSceneAsync();

// When the scene is done loading...
CreateSGBRequiredGameObjects();
// When the scene is done loading, create required objects
CreateSGBRequiredGameObjects(out UnityEntry createdEntryPoint);
CreateCustomRequiredGameObjects();

// If we have a save file to load into, jump directly into the game.
if (saveFile != -1)
{
// Custom initialize SGB, ahead of Start() call, so that we can...
InitializeSGBEntry(createdEntryPoint);

// ...reset SGB's state, load the first save file, and boot DIRECTLY into the map scene.
UnityEntry.game.DoReset(true, false);
UnityEntry.game.DoLoad(0);
UnityEntry.game.ChangeScene(Yukar.Engine.GameMain.Scenes.MAP);
}
}

// Unload from current Smile Builder Game, and load into a specific scene
Expand Down Expand Up @@ -87,15 +102,15 @@ private static async Task LoadSceneTrueAsync(string sceneName)
}

// Construct the GameObjects that SGB needs to initialize
private static void CreateSGBRequiredGameObjects()
private static void CreateSGBRequiredGameObjects(out UnityEntry entry)
{
CreateSGBRequiredGameObjectsHelper_MainCamera();
CreateSGBRequiredGameObjectsHelper_ParentAndDummy("MapScene");
CreateSGBRequiredGameObjectsHelper_ParentAndDummy("BattleScene");
CreateSGBRequiredGameObjectsHelper_ParentAndDummy("Sound");
CreateSGBRequiredGameObjectsHelper_ParentAndDummy("Template");
CreateSGBRequiredGameObjectsHelper_UnityAds();
CreateSGBRequiredGameObjectsHelper_UnityEntry();
CreateSGBRequiredGameObjectsHelper_UnityEntry(out entry);
}

// Helper class for CreateSGBRequiredGameObjects. Creates the Main Camera
Expand All @@ -115,13 +130,15 @@ private static void CreateSGBRequiredGameObjectsHelper_MainCamera()
}

// Helper class for CreateSGBRequiredGameObjects. Creates the Entry object
private static void CreateSGBRequiredGameObjectsHelper_UnityEntry()
private static void CreateSGBRequiredGameObjectsHelper_UnityEntry(out UnityEntry entry)
{
// Create the Entry Object
GameObject unityEntryObject = new GameObject(
"Entry",
typeof(UnityEntry)
);

entry = unityEntryObject.GetComponent<UnityEntry>();
}

// Helper class for CreateSGBRequiredGameObjects. Creates an empty GameObject with a dummy child object,
Expand Down Expand Up @@ -172,5 +189,40 @@ private static bool DoesSGBSubpathExist(string gameSubpath)
// If it has been loaded correctly, it shouldn't be null.
return assetFile != null;
}

private static void InitializeSGBEntry(UnityEntry entry)
{
UnityEntry.self = entry;

SharpKmyGfx.Render.InitializeRender();
Yukar.Common.UnityUtil.Initialize();
#if !UNITY_EDITOR
Debug.unityLogger.logEnabled = false;
#endif
#if UNITY_IOS || UNITY_ANDROID
UnityResolution.Start();
#endif //UNITY_IOS || UNITY_ANDROID

Yukar.Common.FileUtil.language = Application.systemLanguage.ToString();

Screen.sleepTimeout = SleepTimeout.NeverSleep;

UnityEntry.InitializeDir();

Yukar.Common.FileUtil.initialize();

Yukar.Common.Catalog.createDlcList(false);
Yukar.Common.Catalog catalog = new Yukar.Common.Catalog();
catalog.load(false);

UnityEntry.game = new Yukar.Engine.GameMain();
UnityEntry.game.initialize();

UnityAdsManager.Initialize(UnityEntry.game);

entry.didInit = true;
entry.didInitWithGame = UnityEntry.game;
entry.didInitWithSelf = entry;
}
}
}
45 changes: 34 additions & 11 deletions Runtime/src/UnityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class UnityEntry : MonoBehaviour

// Icy Override End

#if IMOD
public static bool overrideInit = false;
#endif


// Custom overrides
static public bool mOverridesOn = true; // Set false to skip all custom overrides and true to apply overrides below
Expand All @@ -31,18 +35,21 @@ public class UnityEntry : MonoBehaviour
// Set custom color of hero name label (default Microsoft.Xna.Framework.Color.White)
static public Microsoft.Xna.Framework.Color mHeroesNamesDecorationFontColor = Microsoft.Xna.Framework.Color.DeepPink;
// End of custom overrides
static protected GameMain sGame = null;
static internal GameMain game { get { return sGame; } }
static protected UnityEntry sSelf = null;
static internal UnityEntry self { get { return sSelf; } }

public static GameMain game;
public static UnityEntry self;

public bool didInit = false;
public GameMain didInitWithGame = null;
public UnityEntry didInitWithSelf = null;

private static Texture2D sFrameBuffer;
#if UNITY_IOS || UNITY_ANDROID
int mUpdateSkipCount = 2;//解像度変更のため2フレーム待つ
#endif


internal static void InitializeDir()
public static void InitializeDir()
{
// Icy Override Start
#if UNITY_SWITCH && !UNITY_EDITOR
Expand All @@ -68,10 +75,12 @@ internal static void InitializeDir()
// Use this for initialization
void Start()
{
if (overrideInit || didInit) return;

if (game != null) return;
SharpKmyGfx.Render.InitializeRender();
UnityUtil.Initialize();
sSelf = this;
self = this;
#if !UNITY_EDITOR
Debug.unityLogger.logEnabled = false;
#endif
Expand All @@ -96,21 +105,35 @@ void Start()
Yukar.Common.GameData.SystemData.sDefaultSeVolume = catalog.getGameSettings().defaultSeVolume;
#endif

sGame = new GameMain();
game = new GameMain();
game.initialize();

UnityAdsManager.Initialize(game);

didInit = true;
didInitWithGame = game;
didInitWithSelf = this;
}

// Icy Override Start
private void OnDestroy()
{
if (!didInit) return;

// Deinit everything we can
// FOR THE FUTURE - fix a case here where we create a deinit for the UnityResolution class
game.finalize();
sGame = null;
UnityUtil.DeInit();
sSelf = null;

if (game == didInitWithGame)
{
game.finalize();
game = null;
}

if (self == didInitWithSelf)
{
self = null;
}
}
// Icy Override End

Expand Down Expand Up @@ -153,7 +176,7 @@ void OnGUI()

void OnApplicationQuit()
{
sSelf = null;
self = null;
}

public static bool IsImportMapScene()
Expand Down

0 comments on commit 48d9c34

Please sign in to comment.