Skip to content

Commit

Permalink
FF8: Update metadata file when a save file is created (julianxhokaxhi…
Browse files Browse the repository at this point in the history
…u#655)

Prevent file loss when the game is closed and the launcher is re-opened
  • Loading branch information
myst6re authored Feb 15, 2024
1 parent 22b64bd commit 405acdc
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 31 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ Write-Output "_CHANGELOG_VERSION=${env:_CHANGELOG_VERSION}" >> ${env:GITHUB_ENV}

# Install CMake
Write-Output "Installing cmake v${env:_WINGET_CMAKE}..."
winget install Kitware.CMake --version ${env:_WINGET_CMAKE} --silent --accept-source-agreements --accept-package-agreements --disable-interactivity | out-null
winget install Kitware.CMake --version ${env:_WINGET_CMAKE} --silent --uninstall-previous --accept-source-agreements --accept-package-agreements --disable-interactivity --force | out-null
cmake --version

# Install Powershell
Write-Output "Installing powershell v${env:_WINGET_POWERSHELL}..."
winget install Microsoft.PowerShell --version ${env:_WINGET_POWERSHELL} --silent --accept-source-agreements --accept-package-agreements --disable-interactivity | out-null
winget install Microsoft.PowerShell --version ${env:_WINGET_POWERSHELL} --silent --uninstall-previous --accept-source-agreements --accept-package-agreements --disable-interactivity --force | out-null
pwsh --version

# Install Visual Studio Enterprise
Write-Output "Installing VisualStudio 2022 Enterprise v${env:_WINGET_VS2022}..."
winget install Microsoft.VisualStudio.2022.Enterprise --version ${env:_WINGET_VS2022} --silent --accept-source-agreements --accept-package-agreements --disable-interactivity | out-null
winget install Microsoft.VisualStudio.2022.Enterprise --version ${env:_WINGET_VS2022} --silent --accept-source-agreements --accept-package-agreements --disable-interactivity --force | out-null

# Load vcvarsall environment for x86
$vcvarspath = &"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -prerelease -latest -property InstallationPath
Expand All @@ -82,8 +82,8 @@ Get-Content "$env:temp\vcvars.txt" | Foreach-Object {
[Environment]::SetEnvironmentVariable('VCPKG_ROOT','')

# Add Github Packages registry
nuget sources add -Name github -Source "https://nuget.pkg.github.com/${env:GITHUB_REPOSITORY_OWNER}/index.json" -Username ${env:GITHUB_REPOSITORY_OWNER} -Password ${env:GITHUB_PACKAGES_PAT} -StorePasswordInClearText
nuget setApiKey ${env:GITHUB_PACKAGES_PAT} -Source "https://nuget.pkg.github.com/${env:GITHUB_REPOSITORY_OWNER}/index.json"
nuget sources add -Name github -Source "https://nuget.pkg.github.com/julianxhokaxhiu/index.json" -Username ${env:GITHUB_REPOSITORY_OWNER} -Password ${env:GITHUB_PACKAGES_PAT} -StorePasswordInClearText
nuget setApiKey ${env:GITHUB_PACKAGES_PAT} -Source "https://nuget.pkg.github.com/julianxhokaxhiu/index.json"
nuget sources list

# Vcpkg setup
Expand Down
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
- Input: Fix mapping of gamepad buttons when modified in the game ( https://github.com/julianxhokaxhiu/FFNx/pull/641 )
- Input: Fix inverted analog up/down direction for non-XInput controllers ( https://github.com/julianxhokaxhiu/FFNx/pull/647 )

## FF8 Steam

- Misc: Rewrite metadata file when a save file is created to prevent save file loss ( https://github.com/julianxhokaxhiu/FFNx/pull/655 )

# 1.17.1

- Full commit list since last stable release: https://github.com/julianxhokaxhiu/FFNx/compare/1.17.0...1.17.1
Expand Down
4 changes: 2 additions & 2 deletions docs/ff8/mods/external_textures.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ Path: `{mod_path}\field\mapdata\{map name}\{map name}.png`

Example: `mods\Textures\field\mapdata\bccent_1\bccent_1.png`

#### Legacy support for Tomberry's mods
#### Legacy support for Tonberry's mods

There is an alternative way to override maps, but on some edge cases it can be buggy.
This is compatible with mods made for [Tomberry](https://forums.qhimm.com/index.php?topic=15945.0).
This is compatible with mods made for [Tonberry](https://forums.qhimm.com/index.php?topic=15945.0).

Path: `{mod_path}\field\mapdata\{first two letters of map name}\{map name}\{map name}_{number}.png`

Expand Down
2 changes: 1 addition & 1 deletion misc/FFNx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ save_textures = false

# Dump internal legacy textures to PNG files in the mod_path
# FF7: There is currently no legacy format
# FF8: Field backgrounds will be saved in Tomberry's format, instead of FFNx one
# FF8: Field backgrounds will be saved in Tonberry's format, instead of FFNx one
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
save_textures_legacy = false

Expand Down
4 changes: 4 additions & 0 deletions src/ff8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,10 @@ struct ff8_externals
ff8_menu_callback *menu_callbacks;
uint32_t menu_config_controller;
uint32_t main_menu_render_sub_4E5550;
uint32_t main_menu_controller;
uint32_t menu_chocobo_world_controller;
uint32_t create_save_file_sub_4C6E50;
uint32_t create_save_chocobo_world_file_sub_4C6620;
uint32_t get_text_data;
uint32_t sub_4BE4D0;
uint32_t sub_4BECC0;
Expand Down
2 changes: 1 addition & 1 deletion src/ff8/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ TexturePacker::TextureTypes TextureBackground::drawToImage(
const uint8_t imgScale = _texture.scale();
const uint32_t imgWidth = mip.m_width / imgScale, imgHeight = mip.m_height / imgScale;

// Tomberry way
// Tonberry way
if (_vramPageId >= 0) {
const int realOffsetX = (_vramPageId * TEXTURE_WIDTH_BPP16 - offsetX) / (4 >> uint16_t(targetBpp));
const int vramPageIdTarget = realOffsetX / TEXTURE_WIDTH_BPP16;
Expand Down
23 changes: 18 additions & 5 deletions src/ff8/texture_packer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ bool TexturePacker::setTextureBackground(const char *name, int x, int y, int w,
return tex.mod() != nullptr;
}

bool TexturePacker::setTextureRedirection(const char *name, const TextureInfos &oldTexture, const TextureInfos &newTexture, uint32_t *imageData)
void TexturePacker::setTextureRedirection(const char *name, const TextureInfos &oldTexture, const TextureInfos &newTexture, const Tim &tim)
{
if (trace_all || trace_vram) ffnx_trace("TexturePacker::%s: %s old=(%d, %d, %d, %d) <= new=(%d, %d, %d, %d)\n", __func__, name,
oldTexture.x(), oldTexture.y(), oldTexture.w(), oldTexture.h(),
Expand All @@ -199,7 +199,7 @@ bool TexturePacker::setTextureRedirection(const char *name, const TextureInfos &
{
ffnx_warning("TexturePacker::%s cannot find original texture of redirection\n", __func__);

return false;
return;
}

auto it = _textures.find(textureId);
Expand All @@ -208,7 +208,7 @@ bool TexturePacker::setTextureRedirection(const char *name, const TextureInfos &
{
ffnx_warning("TexturePacker::%s cannot find original texture of redirection 2\n", __func__);

return false;
return;
}

if (trace_all || trace_vram) ffnx_trace("TexturePacker::%s: found %s\n", __func__, it->second.printableName());
Expand All @@ -233,13 +233,26 @@ bool TexturePacker::setTextureRedirection(const char *name, const TextureInfos &
if (trace_all || trace_vram) ffnx_trace("TexturePacker::%s: use internal texture\n", __func__);

delete mod;

uint32_t *imageData = (uint32_t*)driver_malloc(newTexture.pixelW() * newTexture.h() * 4);

if (!imageData)
{
return;
}

if (!tim.toRGBA32MultiPaletteGrid(imageData, 4, 4, 0, 4, true))
{
driver_free(imageData);

return;
}

// Use internal texture
red.setMod(new TextureRawImage(red, imageData, newTexture.pixelW(), newTexture.h()));
}

_textures[textureId].setRedirection(redirectionTextureId, red);

return true;
}

void TexturePacker::animateTextureByCopy(int sourceXBpp2, int sourceY, int sourceWBpp2, int sourceH, int targetXBpp2, int targetY)
Expand Down
2 changes: 1 addition & 1 deletion src/ff8/texture_packer.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class TexturePacker {
bool setTexture(const char *name, const TextureInfos &texture, const TextureInfos &palette = TextureInfos(), int textureCount = -1, bool clearOldTexture = true);
bool setTextureBackground(const char *name, int x, int y, int w, int h, const std::vector<Tile> &mapTiles, int bgTexId = -1, const char *extension = nullptr, char *found_extension = nullptr);
// Override a part of the VRAM from another part of the VRAM, typically with biggest textures (Worldmap)
bool setTextureRedirection(const char *name, const TextureInfos &oldTexture, const TextureInfos &newTexture, uint32_t *imageData);
void setTextureRedirection(const char *name, const TextureInfos &oldTexture, const TextureInfos &newTexture, const Tim &tim);
void animateTextureByCopy(int sourceXBpp2, int y, int sourceWBpp2, int sourceH, int targetXBpp2, int targetY);
void forceCurrentPalette(int xBpp2, int y, int8_t paletteId);
void clearTiledTexs();
Expand Down
17 changes: 1 addition & 16 deletions src/ff8/vram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,22 +816,7 @@ void ff8_wm_texl_palette_upload_vram(int16_t *pos_and_size, uint8_t *texture_buf
}

// Redirect internal texl textures
uint32_t image_data_size = newTexture.pixelW() * newTexture.h() * 4;
uint32_t *image = (uint32_t*)driver_malloc(image_data_size);

if (image)
{
if (! tim.toRGBA32MultiPaletteGrid(image, 4, 4, 0, 4, true))
{
driver_free(image);
return;
}

if (! texturePacker.setTextureRedirection(next_texture_name, oldTexture, newTexture, image))
{
driver_free(image);
}
}
texturePacker.setTextureRedirection(next_texture_name, oldTexture, newTexture, tim);

*next_texture_name = '\0';

Expand Down
4 changes: 4 additions & 0 deletions src/ff8_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ void ff8_find_externals()
ff8_externals.menu_callbacks = (ff8_menu_callback *)get_absolute_value(ff8_externals.sub_4BDB30, 0x11);
ff8_externals.menu_config_controller = get_absolute_value(uint32_t(ff8_externals.menu_callbacks[8].func), 0x8);
ff8_externals.main_menu_render_sub_4E5550 = get_absolute_value(uint32_t(ff8_externals.menu_callbacks[16].func), 0x3);
ff8_externals.main_menu_controller = get_absolute_value(uint32_t(ff8_externals.menu_callbacks[16].func), 0x8);
ff8_externals.menu_chocobo_world_controller = get_absolute_value(uint32_t(ff8_externals.menu_callbacks[27].func), 0xB);
ff8_externals.create_save_file_sub_4C6E50 = get_relative_call(ff8_externals.main_menu_controller, 0xF8D);
ff8_externals.create_save_chocobo_world_file_sub_4C6620 = get_relative_call(ff8_externals.menu_chocobo_world_controller, 0x9F6);
ff8_externals.get_text_data = get_relative_call(ff8_externals.main_menu_render_sub_4E5550, 0x203);
ff8_externals.sub_4BE4D0 = get_relative_call(ff8_externals.sub_4B3410, 0x68);
ff8_externals.sub_4BECC0 = get_relative_call(ff8_externals.sub_4BE4D0, 0x39);
Expand Down
36 changes: 36 additions & 0 deletions src/ff8_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "gamehacks.h"
#include "vibration.h"
#include "ff8/file.h"
#include "metadata.h"

unsigned char texture_reload_fix1[] = {0x5B, 0x5F, 0x5E, 0x5D, 0x81, 0xC4, 0x10, 0x01, 0x00, 0x00};
unsigned char texture_reload_fix2[] = {0x5F, 0x5E, 0x5D, 0x5B, 0x81, 0xC4, 0x8C, 0x00, 0x00, 0x00};
Expand Down Expand Up @@ -863,6 +864,30 @@ char *ff8_get_text_cached_load_game(int pool_id, int cat_id, int text_id, int a4
return ff8_get_text_cached(pool_id, cat_id, text_id, a4, load_game_text_cache);
}

int ff8_create_save_file(int a1, int a2)
{
if (trace_all) ffnx_trace("%s\n", __func__);

int ret = ((int(*)(int,int))ff8_externals.create_save_file_sub_4C6E50)(a1, a2);

metadataPatcher.apply();

return ret;
}

int ff8_create_save_file_chocobo_world(int unused, int data_source, int offset, size_t size)
{
if (trace_all) ffnx_trace("%s\n", __func__);

int ret = ((int(*)(int,int,int,size_t))ff8_externals.create_save_chocobo_world_file_sub_4C6620)(unused, data_source, offset, size);

if (ret > 0) {
metadataPatcher.apply();
}

return ret;
}

void ff8_init_hooks(struct game_obj *_game_object)
{
struct ff8_game_obj *game_object = (struct ff8_game_obj *)_game_object;
Expand Down Expand Up @@ -904,6 +929,7 @@ void ff8_init_hooks(struct game_obj *_game_object)
ff8_externals.d3dcaps[2] = true;
ff8_externals.d3dcaps[3] = true;

// Fix save format
if (version == VERSION_FF8_12_FR_NV || version == VERSION_FF8_12_SP_NV || version == VERSION_FF8_12_IT_NV)
{
unsigned char ff8fr_savefix1[] = "\xC0\xEA\x03\x8A\x41\x6D\x80\xE2"
Expand Down Expand Up @@ -946,6 +972,16 @@ void ff8_init_hooks(struct game_obj *_game_object)
patch_code_byte(ff8_externals.sub_54FDA0 + 0x1DB, 0x70);
}

// Update the metadata file when a save file is modified
if (steam_edition)
{
replace_call(ff8_externals.main_menu_controller + 0xF8D, ff8_create_save_file);
replace_call(ff8_externals.menu_chocobo_world_controller + 0x9F6, ff8_create_save_file_chocobo_world);
replace_call(ff8_externals.menu_chocobo_world_controller + 0xFA3, ff8_create_save_file_chocobo_world);
replace_call(ff8_externals.menu_chocobo_world_controller + 0x11BB, ff8_create_save_file_chocobo_world);
replace_call(ff8_externals.menu_chocobo_world_controller + 0x13EC, ff8_create_save_file_chocobo_world);
}

// don't set system speaker config to stereo
memset_code(common_externals.directsound_create + 0x6D, 0x90, 34);

Expand Down

0 comments on commit 405acdc

Please sign in to comment.