From cc38fa1d5a5ae38538901e00e76c293bcee0cd12 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Fri, 6 Sep 2013 23:36:54 -0400 Subject: [PATCH] Profile: add custom load/save hooks Themes can use these to implement custom per-profile data, such as SpeedMods.txt. For memory cards, they will be executed while the card is mounted and should read/write everything they will need at that point. --- Themes/_fallback/metrics.ini | 2 ++ src/Profile.cpp | 46 +++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Themes/_fallback/metrics.ini b/Themes/_fallback/metrics.ini index 3bc79c7a7b..94557d55df 100644 --- a/Themes/_fallback/metrics.ini +++ b/Themes/_fallback/metrics.ini @@ -1183,6 +1183,8 @@ JudgmentTransformCommand=%JudgmentTransformSharedCommand [Profile] ShowCoinData=true UnlockAuthString="" +CustomLoadFunction=function(profile, dir) end +CustomSaveFunction=function(profile, dir) end [RadarValues] WriteComplexValues=true diff --git a/src/Profile.cpp b/src/Profile.cpp index 20d9236760..26d73539c3 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -876,7 +876,31 @@ ProfileLoadResult Profile::LoadAllFromDir( RString sDir, bool bRequireSignature return ProfileLoadResult_FailedTampered; LOG->Trace( "Done." ); - return LoadStatsXmlFromNode( &xml ); + ProfileLoadResult ret = LoadStatsXmlFromNode(&xml); + if (ret != ProfileLoadResult_Success) + return ret; + + /* Get the theme's custom load function: + * [Profile] + * CustomLoadFunction=function(profile, profileDir) ... end + */ + Lua *L = LUA->Get(); + LuaReference customLoadFunc = THEME->GetMetricR("Profile", "CustomLoadFunction"); + customLoadFunc.PushSelf(L); + ASSERT_M(!lua_isnil(L, -1), "CustomLoadFunction not defined"); + + // Pass profile and profile directory as arguments + this->PushSelf(L); + LuaHelpers::Push(L, sDir); + + // Run it + RString sError; + if (!LuaHelpers::RunScriptOnStack(L, sError, 2, 0)) + LOG->Warn("Error running CustomLoadFunction: %s", sError.c_str()); + + LUA->Release(L); + + return ProfileLoadResult_Success; } ProfileLoadResult Profile::LoadStatsXmlFromNode( const XNode *xml, bool bIgnoreEditable ) @@ -936,6 +960,26 @@ bool Profile::SaveAllToDir( RString sDir, bool bSignData ) const FILEMAN->CreateDir( sDir + SCREENSHOTS_SUBDIR ); FILEMAN->CreateDir( sDir + RIVAL_SUBDIR ); + /* Get the theme's custom save function: + * [Profile] + * CustomSaveFunction=function(profile, profileDir) ... end + */ + Lua *L = LUA->Get(); + LuaReference customSaveFunc = THEME->GetMetricR("Profile", "CustomSaveFunction"); + customSaveFunc.PushSelf(L); + ASSERT_M(!lua_isnil(L, -1), "CustomSaveFunction not defined"); + + // Pass profile and profile directory as arguments + const_cast(this)->PushSelf(L); + LuaHelpers::Push(L, sDir); + + // Run it + RString sError; + if (!LuaHelpers::RunScriptOnStack(L, sError, 2, 0)) + LOG->Warn("Error running CustomSaveFunction: %s", sError.c_str()); + + LUA->Release(L); + return bSaved; }