Skip to content

Commit

Permalink
Implement PAL3A SceneEnableFogCommand + code refactoring
Browse files Browse the repository at this point in the history
- Implement PAL3A SceneEnableFogCommand
- Improves logging
- Object 46 in scene M03-1 should block player
-
  • Loading branch information
0x7c13 committed Aug 3, 2023
1 parent 7b80432 commit c3893fb
Show file tree
Hide file tree
Showing 64 changed files with 384 additions and 336 deletions.
4 changes: 2 additions & 2 deletions Assets/Scripts/Core/DataLoader/AudioClipLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static IEnumerator LoadAudioClipAsync(string filePath,

if (request.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError($"Failed to load {url} with error: {request.error}");
Debug.LogError($"[{nameof(AudioClipLoader)}] Failed to load {url} with error: {request.error}");
}
else
{
Expand All @@ -49,7 +49,7 @@ public static IEnumerator LoadAudioClipAsync(string filePath,
}
catch (Exception ex)
{
Debug.LogError($"Failed to get audio clip: {filePath} with exception: {ex}");
Debug.LogError($"[{nameof(AudioClipLoader)}] Failed to get audio clip: {filePath} with exception: {ex}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Core/DataLoader/DxtTextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private bool IsValidDxtFile(BinaryReader reader)
{
var header = new string(reader.ReadChars(4));
if (header == "DDS ") return true;
Debug.Log($"Invalid DXT header: {header}");
Debug.Log($"[{nameof(DxtTextureLoader)}] Invalid DXT header: {header}");
return false;
}

Expand Down
7 changes: 5 additions & 2 deletions Assets/Scripts/Core/DataReader/Cpk/CpkArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void ExtractToInternal(string outputFolder, IEnumerable<CpkEntry> nodes)
foreach (CpkEntry node in nodes)
{
var relativePath = node.VirtualPath.Replace(
CpkConstants.DirectorySeparator, Path.DirectorySeparatorChar);
CpkConstants.DirectorySeparatorChar, Path.DirectorySeparatorChar);

if (node.IsDirectory)
{
Expand Down Expand Up @@ -313,7 +313,10 @@ private IEnumerable<CpkEntry> GetChildren(uint fatherCrc, string rootPath = "")
yield break;
}

if (rootPath != string.Empty) rootPath += CpkConstants.DirectorySeparator;
if (!string.IsNullOrEmpty(rootPath))
{
rootPath += CpkConstants.DirectorySeparatorChar;
}

foreach (var childCrc in _fatherCrcToChildCrcTableIndexMap[fatherCrc])
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Core/DataReader/Cpk/CpkConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Core.DataReader.Cpk
{
public static class CpkConstants
{
public const char DirectorySeparator = '\\';
public const char DirectorySeparatorChar = '\\';

public const string FileExtension = ".cpk";
}
Expand Down
5 changes: 4 additions & 1 deletion Assets/Scripts/Core/DataReader/Cpk/CrcHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Core.DataReader.Cpk
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

/// <summary>
/// Crc32 hash provider.
Expand Down Expand Up @@ -39,6 +40,8 @@ public void Init()
}

_initialized = true;

Debug.Log($"[{nameof(Crc32Hash)}] CRC32 table initialized.");
}

public uint Compute(string str, int codepage, bool useCache = true)
Expand All @@ -64,7 +67,7 @@ public uint Compute(string str, int codepage, bool useCache = true)

private unsafe uint ComputeInternal(byte[] data)
{
if (!_initialized) throw new InvalidOperationException("CrcHash not initialized yet.");
if (!_initialized) throw new InvalidOperationException($"{nameof(Crc32Hash)} not initialized yet.");

if (data == null || data.Length == 0 || data[0] == 0)
{
Expand Down
37 changes: 22 additions & 15 deletions Assets/Scripts/Core/FileSystem/CpkFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public string GetRootPath()
/// <param name="codepage">Codepage CPK file uses for encoding text info</param>
public void Mount(string cpkFileRelativePath, int codepage)
{
var cpkFileName = Path.GetFileName(cpkFileRelativePath).ToLower();
var cpkFileName = Utility.GetFileName(cpkFileRelativePath, Path.DirectorySeparatorChar).ToLower();

if (_cpkArchives.ContainsKey(cpkFileName))
{
Debug.LogWarning($"{cpkFileRelativePath} already mounted.");
Debug.LogWarning($"[{nameof(CpkFileSystem)}] {cpkFileRelativePath} already mounted.");
return;
}

Debug.Log($"CpkFileSystem mounting: {_rootPath + cpkFileRelativePath}");
Debug.Log($"[{nameof(CpkFileSystem)}] Mounting: {_rootPath + cpkFileRelativePath}");
var cpkArchive = new CpkArchive(_rootPath + cpkFileRelativePath, _crcHash, codepage);
cpkArchive.Init();
_cpkArchives[cpkFileName] = cpkArchive;
Expand All @@ -76,7 +76,7 @@ public void Mount(string cpkFileRelativePath, int codepage)
/// <summary>
/// Check if file exists in any of the segmented archives using virtual path.
/// </summary>
/// <param name="fileVirtualPath">File virtual path {Cpk file name}.cpk\{File relative path inside archive}</param>
/// <param name="fileVirtualPath">File virtual path {Cpk file name}\{File relative path inside archive}</param>
/// <param name="segmentedArchiveName">Name of the segmented archive if exists</param>
/// <returns>True if file exists in segmented archive</returns>
public bool FileExistsInSegmentedArchive(string fileVirtualPath, out string segmentedArchiveName)
Expand Down Expand Up @@ -108,7 +108,7 @@ public bool FileExistsInSegmentedArchive(string fileVirtualPath, out string segm
/// <summary>
/// Check if file exists in the archive using virtual path.
/// </summary>
/// <param name="fileVirtualPath">File virtual path {Cpk file name}.cpk\{File relative path inside archive}
/// <param name="fileVirtualPath">File virtual path {Cpk file name}\{File relative path inside archive}
/// Example: music.cpk\music\PI01.mp3</param>
/// <returns>True if file exists</returns>
public bool FileExists(string fileVirtualPath)
Expand Down Expand Up @@ -168,12 +168,12 @@ public void LoadArchiveIntoMemory(string cpkFileName)
{
if (_cpkArchives.ContainsKey(cpkFileName.ToLower()))
{
Debug.Log($"File system caching {cpkFileName} into memory.");
Debug.Log($"[{nameof(CpkFileSystem)}] Caching {cpkFileName} archive into memory.");
_cpkArchives[cpkFileName.ToLower()].LoadArchiveIntoMemory();
}
else
{
throw new Exception($"{cpkFileName} not mounted yet.");
throw new Exception($"{cpkFileName} archive not mounted yet.");
}
}

Expand All @@ -184,12 +184,12 @@ public void DisposeInMemoryArchive(string cpkFileName)
{
if (_cpkArchives.ContainsKey(cpkFileName.ToLower()))
{
Debug.Log($"File system disposing in-memory cache: {cpkFileName}");
Debug.Log($"[{nameof(CpkFileSystem)}] Disposing in-memory cache: {cpkFileName}");
_cpkArchives[cpkFileName.ToLower()].DisposeInMemoryArchive();
}
else
{
throw new Exception($"{cpkFileName} not mounted yet.");
throw new Exception($"{cpkFileName} archive not mounted yet.");
}
}

Expand Down Expand Up @@ -219,7 +219,7 @@ public void ExtractTo(string outputFolder)
}

cpkArchive.ExtractTo(outputDir);
Debug.Log($"{cpkFileName} extracted to {outputDir}");
Debug.Log($"[{nameof(CpkFileSystem)}] {cpkFileName} extracted to {outputDir}");
}
}

Expand All @@ -236,7 +236,7 @@ public IEnumerable<string> Search(string keyword = "")
{
var rootNodes = archive.Value.GetRootEntries();
results.Add(from result in SearchInternal(rootNodes, keyword)
select archive.Key + CpkConstants.DirectorySeparator + result);
select archive.Key + CpkConstants.DirectorySeparatorChar + result);
});

var resultList = new List<string>();
Expand All @@ -247,14 +247,21 @@ public IEnumerable<string> Search(string keyword = "")
return resultList;
}

/// <summary>
/// Parse file virtual path into cpk file name and relative virtual path.
/// </summary>
/// <param name="fullVirtualPath">File virtual path {Cpk file name}\{File relative path inside archive}</param>
/// <param name="cpkFileName">{Cpk file name}</param>
/// <param name="relativeVirtualPath">{File relative path inside archive}</param>
private void ParseFileVirtualPath(string fullVirtualPath, out string cpkFileName, out string relativeVirtualPath)
{
if (!fullVirtualPath.Contains(CpkConstants.DirectorySeparator))
if (!fullVirtualPath.Contains(CpkConstants.DirectorySeparatorChar))
{
throw new ArgumentException($"File virtual path is invalid: {fullVirtualPath}.");
throw new ArgumentException($"[{nameof(CpkFileSystem)}] File virtual path is invalid: {fullVirtualPath}.");
}
cpkFileName = fullVirtualPath[..fullVirtualPath.IndexOf(CpkConstants.DirectorySeparator)].ToLower();
relativeVirtualPath = fullVirtualPath[(fullVirtualPath.IndexOf(CpkConstants.DirectorySeparator) + 1)..];

cpkFileName = fullVirtualPath[..fullVirtualPath.IndexOf(CpkConstants.DirectorySeparatorChar)].ToLower();
relativeVirtualPath = fullVirtualPath[(fullVirtualPath.IndexOf(CpkConstants.DirectorySeparatorChar) + 1)..];
}

private IEnumerable<string> SearchInternal(IEnumerable<CpkEntry> nodes, string keyword)
Expand Down
5 changes: 2 additions & 3 deletions Assets/Scripts/Core/FileSystem/ICpkFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Core.FileSystem
{
using System.Collections.Generic;
using System.IO;

/// <summary>
/// File system wrapper for CPack archives
Expand All @@ -29,15 +28,15 @@ public interface ICpkFileSystem
/// <summary>
/// Check if file exists in any of the segmented archives using virtual path.
/// </summary>
/// <param name="fileVirtualPath">File virtual path {Cpk file name}.cpk\{File relative path inside archive}</param>
/// <param name="fileVirtualPath">File virtual path {Cpk file name}\{File relative path inside archive}</param>
/// <param name="segmentedArchiveName">Name of the segmented archive if exists</param>
/// <returns>True if file exists in segmented archive</returns>
public bool FileExistsInSegmentedArchive(string fileVirtualPath, out string segmentedArchiveName);

/// <summary>
/// Check if file exists in the archive using virtual path.
/// </summary>
/// <param name="fileVirtualPath">File virtual path {Cpk file name}.cpk\{File relative path inside archive}
/// <param name="fileVirtualPath">File virtual path {Cpk file name}\{File relative path inside archive}
/// Example: music.cpk\music\PI01.mp3</param>
/// <returns>True if file exists</returns>
public bool FileExists(string fileVirtualPath);
Expand Down
15 changes: 12 additions & 3 deletions Assets/Scripts/Core/Services/ServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public T Get<T>()
{
if (!_services.ContainsKey(typeof(T)))
{
string error = $"{typeof(T)} not registered with {GetType().Name}";
string error = $"[{nameof(ServiceLocator)}] {typeof(T)} is not registered with name: {GetType().Name}";
Debug.LogError(error);
throw new InvalidOperationException(error);
}
Expand All @@ -50,9 +50,13 @@ public void Register<T>(T service)

if (_services.ContainsKey(typeof(T)))
{
Debug.LogWarning($"{typeof(T)} already registered.");
Debug.LogWarning($"[{nameof(ServiceLocator)}] {typeof(T)} already registered.");
return;
}
else
{
Debug.Log($"[{nameof(ServiceLocator)}] Service type {typeof(T)} registered.");
}

_services.Add(typeof(T), service);
}
Expand All @@ -65,9 +69,13 @@ public void Unregister<T>()
{
if (!_services.ContainsKey(typeof(T)))
{
Debug.LogWarning($"Failed to unregister since {typeof(T)} is not registered yet.");
Debug.LogWarning($"[{nameof(ServiceLocator)}] Failed to unregister service since {typeof(T)} is not registered yet.");
return;
}
else
{
Debug.Log($"[{nameof(ServiceLocator)}] Service type {typeof(T)} unregistered.");
}

_services.Remove(typeof(T));
}
Expand All @@ -77,6 +85,7 @@ public void Unregister<T>()
/// </summary>
public void UnregisterAll()
{
Debug.Log($"[{nameof(ServiceLocator)}] All services unregistered.");
_services.Clear();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Core/Utils/FpsCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Core.Utils

public class FpsCounter : MonoBehaviour
{
private const float UPDATE_INTERVAL = 0.2f;
private const float UPDATE_INTERVAL = 0.1f;

private int _frameCount = 0;
private float _deltaTime = 0f;
Expand Down
14 changes: 6 additions & 8 deletions Assets/Scripts/Core/Utils/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,18 @@ public static void Rgb565ToRgb888(ushort rgb565Color, out byte r, out byte g, ou
b = (byte) ((temp / 32 + temp) / 32);
}

public static string GetRelativeDirectoryPath(string filePath,
char directoryPathSeparator = CpkConstants.DirectorySeparator)
public static string GetDirectoryName(string filePath, char directorySeparatorChar)
{
return !filePath.Contains(directoryPathSeparator) ?
return !filePath.Contains(directorySeparatorChar) ?
string.Empty :
filePath[..filePath.LastIndexOf(directoryPathSeparator)];
filePath[..filePath.LastIndexOf(directorySeparatorChar)];
}

public static string GetFileName(string filePath,
char directoryPathSeparator = CpkConstants.DirectorySeparator)
public static string GetFileName(string filePath, char directorySeparatorChar)
{
return !filePath.Contains(directoryPathSeparator) ?
return !filePath.Contains(directorySeparatorChar) ?
string.Empty :
filePath[(filePath.LastIndexOf(directoryPathSeparator)+1)..];
filePath[(filePath.LastIndexOf(directorySeparatorChar)+1)..];
}

public static bool IsHandheldDevice()
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Editor/GameVariantSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void SwitchToPal3()
ApplyPlayerSettingsForVariant("PAL3");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("Switched to PAL3");
Debug.Log($"[{nameof(GameVariantSwitcher)}] Switched to PAL3");
}

#if PAL3
Expand All @@ -51,7 +51,7 @@ public static void SwitchToPal3A()
ApplyPlayerSettingsForVariant("PAL3A");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("Switched to PAL3A");
Debug.Log($"[{nameof(GameVariantSwitcher)}] Switched to PAL3A");
}

#if PAL3
Expand Down
Loading

0 comments on commit c3893fb

Please sign in to comment.