Skip to content

Commit

Permalink
V1.24.3 : fix for issue #115, PNG texture cache isn't always invalida…
Browse files Browse the repository at this point in the history
…ted when a PNG texture is modified
  • Loading branch information
gotmachine committed Jan 24, 2023
1 parent 2eb058f commit f6875b3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion GameData/KSPCommunityFixes/KSPCommunityFixes.version
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"NAME": "KSPCommunityFixes",
"URL": "https://raw.githubusercontent.com/KSPModdingLibs/KSPCommunityFixes/master/GameData/KSPCommunityFixes/KSPCommunityFixes.version",
"DOWNLOAD": "https://github.com/KSPModdingLibs/KSPCommunityFixes/releases",
"VERSION": {"MAJOR": 1, "MINOR": 24, "PATCH": 2, "BUILD": 0},
"VERSION": {"MAJOR": 1, "MINOR": 24, "PATCH": 3, "BUILD": 0},
"KSP_VERSION": {"MAJOR": 1, "MINOR": 12, "PATCH": 5},
"KSP_VERSION_MIN": {"MAJOR": 1, "MINOR": 8, "PATCH": 0},
"KSP_VERSION_MAX": {"MAJOR": 1, "MINOR": 12, "PATCH": 5}
Expand Down
15 changes: 10 additions & 5 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.props" Condition="Exists('..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.props')" />
<Import Project="..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.props" Condition="Exists('..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -90,6 +90,12 @@
<DoNotPublicize Include="Assembly-CSharp:BaseField`1.OnValueModified" />
<DoNotPublicize Include="Assembly-CSharp:SaveUpgradePipeline.SaveUpgradePipeline.OnSetCfgNodeVersion" />
<Publicize Include="UnityEngine.CoreModule:UnityEngine.Object.m_CachedPtr" />
<!--Temporary workaround because v2.2.0 has a bug were it doesn't publicize unless at least one type is publicized-->
<!--see https://github.com/krafs/Publicizer/issues/64 -->
<Publicize Include="UnityEngine.CoreModule:UnityEngine.SystemClock" />
<Publicize Include="mscorlib:System.IO.MonoIO" />
<Publicize Include="mscorlib:System.IO.MonoIOError" />
<Publicize Include="mscorlib:System.IO.MonoIOStat" />
<Publicize Include="mscorlib:System.Reflection.Assembly.GetTypes" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -184,7 +190,6 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<!--Project-specfic configuration-->
<PropertyGroup>
<RepoRootPath>$(SolutionDir)</RepoRootPath>
Expand Down Expand Up @@ -371,8 +376,8 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.props'))" />
<Error Condition="!Exists('..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.targets'))" />
<Error Condition="!Exists('..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.props'))" />
<Error Condition="!Exists('..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.targets'))" />
</Target>
<Import Project="..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.targets" Condition="Exists('..\packages\Krafs.Publicizer.1.0.3\build\Krafs.Publicizer.targets')" />
<Import Project="..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.targets" Condition="Exists('..\packages\Krafs.Publicizer.2.2.0\build\Krafs.Publicizer.targets')" />
</Project>
40 changes: 32 additions & 8 deletions KSPCommunityFixes/Performance/FastLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define DEBUG_TEXTURE_CACHE
#define DEBUG_TEXTURE_CACHE

using DDSHeaders;
using HarmonyLib;
Expand Down Expand Up @@ -1799,7 +1799,8 @@ private class CachedTextureInfo

public string name;
public uint id;
public long creationTime;
public long time;
public long size;
public int width;
public int height;
public int mipCount;
Expand All @@ -1811,7 +1812,7 @@ private class CachedTextureInfo

public CachedTextureInfo() { }

public CachedTextureInfo(UrlFile urlFile, Texture2D texture, bool isNormalMap)
public CachedTextureInfo(UrlFile urlFile, Texture2D texture, bool isNormalMap, long size, long time)
{
name = urlFile.url;
do
Expand All @@ -1823,7 +1824,8 @@ public CachedTextureInfo(UrlFile urlFile, Texture2D texture, bool isNormalMap)
}
while (loader.textureDataIds.Contains(id));

creationTime = File.GetCreationTimeUtc(urlFile.fullPath).ToFileTimeUtc();
this.size = size;
this.time = time;
width = texture.width;
height = texture.height;
mipCount = texture.mipmapCount;
Expand Down Expand Up @@ -1863,9 +1865,7 @@ private static CachedTextureInfo GetCachedTextureInfo(UrlDir.UrlFile file)
if (!loader.textureCacheData.TryGetValue(file.url, out CachedTextureInfo cachedTextureInfo))
return null;

long creationTime = File.GetCreationTimeUtc(file.fullPath).ToFileTimeUtc();

if (cachedTextureInfo.creationTime != creationTime)
if (!GetFileStats(file.fullPath, out long size, out long time) || size != cachedTextureInfo.size || time != cachedTextureInfo.time)
{
loader.textureCacheData.Remove(file.url);
loader.textureDataIds.Remove(cachedTextureInfo.id);
Expand All @@ -1877,9 +1877,33 @@ private static CachedTextureInfo GetCachedTextureInfo(UrlDir.UrlFile file)
return cachedTextureInfo;
}

private static bool GetFileStats(string path, out long size, out long time)
{
MonoIO.GetFileStat(path, out MonoIOStat stat, out MonoIOError error);
if (error == MonoIOError.ERROR_FILE_NOT_FOUND || error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_NOT_READY)
{
size = 0;
time = 0;
return false;
}

size = stat.Length;
time = Math.Max(stat.CreationTime, stat.LastWriteTime);
if (size <= 0 || time <= 0)
return false;

return true;
}

private static void SaveCachedTexture(UrlDir.UrlFile urlFile, Texture2D texture, bool isNormalMap)
{
CachedTextureInfo cachedTextureInfo = new CachedTextureInfo(urlFile, texture, isNormalMap);
if (!GetFileStats(urlFile.fullPath, out long size, out long creationTime))
{
Debug.LogWarning($"[KSPCF] PNG texture '{urlFile.url}' couldn't be cached : IO error");
return;
}

CachedTextureInfo cachedTextureInfo = new CachedTextureInfo(urlFile, texture, isNormalMap, size, creationTime);
cachedTextureInfo.SaveRawTextureData(texture);
loader.textureCacheData.Add(cachedTextureInfo.name, cachedTextureInfo);
loader.textureDataIds.Add(cachedTextureInfo.id);
Expand Down
4 changes: 2 additions & 2 deletions KSPCommunityFixes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.24.2.0")]
[assembly: AssemblyFileVersion("1.24.3.0")]

[assembly: KSPAssembly("KSPCommunityFixes", 1, 24, 2)]
[assembly: KSPAssembly("KSPCommunityFixes", 1, 24, 3)]
[assembly: KSPAssemblyDependency("MultipleModulePartAPI", 1, 0, 0)]
2 changes: 1 addition & 1 deletion KSPCommunityFixes/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Krafs.Publicizer" version="1.0.3" targetFramework="net472" developmentDependency="true" />
<package id="Krafs.Publicizer" version="2.2.0" targetFramework="net472" developmentDependency="true" />
</packages>
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ If doing so in the `Debug` configuration and if your KSP install is modified to

### Changelog

##### 1.24.3
- Fixed [issue #115](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/115) : PNG texture cache isn't always invalidated when a PNG texture is modified

##### 1.24.2
- Fixed [issue #114](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/114) : Drag cubes are incorrectly calculated on modules using `IMultipleDragCube.AssumeDragCubePosition()` due to FastLoader patch not skipping a frame to let the part animation(s) play

Expand Down

0 comments on commit f6875b3

Please sign in to comment.