Skip to content

Commit

Permalink
Ai MainGame class and hooks for AIAPI (#23)
Browse files Browse the repository at this point in the history
* Basic MainGame hooks working with AI

* fixed day counter hook

* potential fix for ExtendedData methods

* GameExtensions.cs AI conversion types

* hook fixes

* better method names to describe AI hooks

* added hooks for OnNewGame

* Use Map instance to get players char file

* Fixed OnHStart

* Fix for isNewGame hook, checking datetime

* converted SaveData.Heroine to AgentData  and NPC to AgentActor

* Reverted test logging back to debug level

* Made ExtendedData methids obsolete with compile error in AI API for now

* Throw if trying to use unimplemented methods
  • Loading branch information
thojmr committed Feb 25, 2021
1 parent 9dde2d7 commit 7921faa
Show file tree
Hide file tree
Showing 10 changed files with 702 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -334,3 +334,6 @@ ASALocalRun/
/lib
PostBuild.bat
*.DotSettings

#Ignore vscode config dir
.vscode
10 changes: 10 additions & 0 deletions src/AIAPI/AIAPI.csproj
Expand Up @@ -97,13 +97,23 @@
<HintPath>..\packages\IllusionLibs.AIGirl.UnityEngine.UIModule.2018.2.21.3\lib\net46\UnityEngine.UIModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sirenix.Serialization, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.AIGirl.Sirenix.Serialization.2020.5.29.2\lib\net46\Sirenix.Serialization.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XUnity.AutoTranslator.Plugin.Core, Version=4.13.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IllusionLibs.XUnity.AutoTranslator.Plugin.Core.4.13.0\lib\net46\XUnity.AutoTranslator.Plugin.Core.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="KoikatuAPI.cs" />
<Compile Include="MainGame\GameApi.cs" />
<Compile Include="MainGame\GameAPI.Hooks.cs" />
<Compile Include="MainGame\GameCustomFunctionController.cs" />
<Compile Include="MainGame\Events\GameSaveLoadEventArgs.cs" />
<Compile Include="MainGame\GameExtensions.cs" />
<Compile Include="MainGame\TestGameFunctionController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/AIAPI/KoikatuAPI.cs
Expand Up @@ -3,6 +3,7 @@
using KKAPI.Chara;
using KKAPI.Maker;
using KKAPI.Studio;
using KKAPI.MainGame;
using Manager;
using UnityEngine;

Expand All @@ -20,6 +21,7 @@ private void Awake()
MakerAPI.Init(insideStudio);
StudioAPI.Init(insideStudio);
CharacterApi.Init();
GameAPI.Init(insideStudio);
}

private void Start()
Expand Down
34 changes: 34 additions & 0 deletions src/AIAPI/MainGame/Events/GameSaveLoadEventArgs.cs
@@ -0,0 +1,34 @@
using System;

namespace KKAPI.MainGame
{
/// <summary>
/// Arguments used with main game save/load events.
/// </summary>
public sealed class GameSaveLoadEventArgs : EventArgs
{
/// <summary>
/// Create a new instance
/// </summary>
public GameSaveLoadEventArgs(string path, string fileName)
{
Path = path;
FileName = fileName;
}

/// <summary>
/// Name of the safe file.
/// </summary>
public string FileName { get; }

/// <summary>
/// Full filename of the save file.
/// </summary>
public string FullFilename => System.IO.Path.Combine(Path, FileName);

/// <summary>
/// Path to which the save file will be written.
/// </summary>
public string Path { get; }
}
}
86 changes: 86 additions & 0 deletions src/AIAPI/MainGame/GameAPI.Hooks.cs
@@ -0,0 +1,86 @@
using System;
using System.Collections;
using HarmonyLib;
using AIProject.SaveData;
using AIProject;
using Sirenix.Serialization;
using AIProject.Scene;
using Manager;

namespace KKAPI.MainGame
{
public static partial class GameAPI
{
private class Hooks
{
public static int lastCurrentDay = 1;//Always starts at 1
public static bool isNewGame = false;

public static void SetupHooks()
{
Harmony.CreateAndPatchAll(typeof(Hooks));
}

[HarmonyPostfix]
[HarmonyPatch(typeof(SaveData), nameof(SaveData.Load), new[] { typeof(string) })]
public static void LoadHook(string fileName)
{
OnGameBeingLoaded("", fileName);
}

[HarmonyPrefix]
[HarmonyPatch(typeof(SaveData), nameof(SaveData.SaveFile), new[] { typeof(string) })]
public static void SaveHook(string path)
{
OnGameBeingSaved(path, "");
}

[HarmonyPrefix]
[HarmonyPatch(typeof(TitleLoadScene), "SetWorldData", typeof(WorldData), typeof(bool))]
public static void TitleLoadScene_SetWorldData(WorldData _worldData, bool isAuto)
{
isNewGame = _worldData?.SaveTime == new DateTime(0);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(MapScene), "OnLoaded")]
public static void MapScene_OnLoaded(MapScene __instance)
{
if (isNewGame) OnNewGame();
}

[HarmonyPostfix]
[HarmonyPatch(typeof(HScene), "SetStartVoice")]
public static void HScene_SetStartVoice(HScene __instance)
{
OnHStart(__instance);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(HScene), "EndProc")]
public static void HScene_EndProc(HScene __instance)
{
OnHEnd(__instance);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(EnvironmentSimulator), nameof(EnvironmentSimulator.SetTimeZone), typeof(AIProject.TimeZone))]
public static void EnvironmentChangeTypeHook(AIProject.TimeZone zone)
{
OnPeriodChange(zone);//morning, day, evening
}

[HarmonyPostfix]
[HarmonyPatch(typeof(EnviroSky), "SetGameTime")]
public static void EnvironmentChangeDayHook(EnviroSky __instance)
{
var currentDay = (int)__instance.currentDay;
if (lastCurrentDay < currentDay)
{
lastCurrentDay = currentDay;
OnDayChange(currentDay);
}
}
}
}
}

0 comments on commit 7921faa

Please sign in to comment.