Skip to content

Commit

Permalink
fix #11, reshuffled some UI and added a button to the main menu/lobby
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAdam committed Jan 6, 2021
1 parent 15eb435 commit 8adeab1
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 131 deletions.
4 changes: 4 additions & 0 deletions Penumbra/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class Configuration : IPluginConfiguration

public bool IsEnabled { get; set; } = true;

public bool ShowAdvanced { get; set; } = false;

public bool DisableFileSystemNotifications { get; set; } = false;

public string CurrentCollection { get; set; } = @"D:/ffxiv/fs_mods/";

public List< string > ModCollections { get; set; } = new();
Expand Down
50 changes: 50 additions & 0 deletions Penumbra/Game/GameUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Runtime.InteropServices;
using Dalamud.Plugin;
using Reloaded.Hooks.Definitions.X64;

namespace Penumbra.Game
{
public class GameUtils
{
private readonly DalamudPluginInterface _pluginInterface;

[Function( CallingConventions.Microsoft )]
public unsafe delegate void* LoadPlayerResourcesPrototype( IntPtr pResourceManager );

[Function( CallingConventions.Microsoft )]
public unsafe delegate void* UnloadPlayerResourcesPrototype( IntPtr pResourceManager );


public LoadPlayerResourcesPrototype LoadPlayerResources { get; private set; }
public UnloadPlayerResourcesPrototype UnloadPlayerResources { get; private set; }

// Object addresses
private IntPtr _playerResourceManagerAddress;
public IntPtr PlayerResourceManagerPtr => Marshal.ReadIntPtr( _playerResourceManagerAddress );

public GameUtils( DalamudPluginInterface pluginInterface )
{
_pluginInterface = pluginInterface;

var scanner = _pluginInterface.TargetModuleScanner;

var loadPlayerResourcesAddress =
scanner.ScanText(
"E8 ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? BA ?? ?? ?? ?? 41 B8 ?? ?? ?? ?? 48 8B 48 30 48 8B 01 FF 50 10 48 85 C0 74 0A " );
var unloadPlayerResourcesAddress =
scanner.ScanText( "41 55 48 81 EC ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 48 33 C4 48 89 84 24 ?? ?? ?? ?? 4C 8B E9 48 83 C1 08" );

_playerResourceManagerAddress = scanner.GetStaticAddressFromSig( "0F 44 FE 48 8B 0D ?? ?? ?? ?? 48 85 C9 74 05" );

LoadPlayerResources = Marshal.GetDelegateForFunctionPointer< LoadPlayerResourcesPrototype >( loadPlayerResourcesAddress );
UnloadPlayerResources = Marshal.GetDelegateForFunctionPointer< UnloadPlayerResourcesPrototype >( unloadPlayerResourcesAddress );
}

public unsafe void ReloadPlayerResources()
{
UnloadPlayerResources( PlayerResourceManagerPtr );
LoadPlayerResources( PlayerResourceManagerPtr );
}
}
}
10 changes: 10 additions & 0 deletions Penumbra/Importer/ImporterState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Penumbra.Importer
{
public enum ImporterState
{
None,
WritingPackToDisk,
ExtractingModFiles,
Done
}
}
26 changes: 26 additions & 0 deletions Penumbra/Importer/MagicTempFileStreamManagerAndDeleterFuckery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.IO;
using Lumina.Data;

namespace Penumbra.Importer
{
public class MagicTempFileStreamManagerAndDeleterFuckery : SqPackStream, IDisposable
{
private readonly FileStream _fileStream;

public MagicTempFileStreamManagerAndDeleterFuckery( FileStream stream ) : base( stream )
{
_fileStream = stream;
}

public new void Dispose()
{
var filePath = _fileStream.Name;

base.Dispose();
_fileStream.Dispose();

File.Delete( filePath );
}
}
}
82 changes: 68 additions & 14 deletions Penumbra/Importer/TexToolsImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,40 @@ internal class TexToolsImport
{
private readonly DirectoryInfo _outDirectory;

private const string TempFileName = "textools-import";
private readonly string _resolvedTempFilePath = null;

public ImporterState State { get; private set; }

public long TotalProgress { get; private set; }
public long CurrentProgress { get; private set; }

public float Progress
{
get
{
if( CurrentProgress != 0 )
{
// ReSharper disable twice RedundantCast
return ( float )CurrentProgress / ( float )TotalProgress;
}

return 0;
}
}

public string CurrentModPack { get; private set; }

public TexToolsImport( DirectoryInfo outDirectory )
{
_outDirectory = outDirectory;
_resolvedTempFilePath = Path.Combine( _outDirectory.FullName, TempFileName );
}

public void ImportModPack( FileInfo modPackFile )
{
CurrentModPack = modPackFile.Name;

switch( modPackFile.Extension )
{
case ".ttmp":
Expand All @@ -33,6 +60,29 @@ public void ImportModPack( FileInfo modPackFile )
ImportV2ModPack( modPackFile );
return;
}

State = ImporterState.Done;
}

private void WriteZipEntryToTempFile( Stream s )
{
var fs = new FileStream( _resolvedTempFilePath, FileMode.Create );
s.CopyTo( fs );
fs.Close();
}

private SqPackStream GetMagicSqPackDeleterStream( ZipFile file, string entryName )
{
State = ImporterState.WritingPackToDisk;

// write shitty zip garbage to disk
var entry = file.GetEntry( entryName );
using var s = file.GetInputStream( entry );

WriteZipEntryToTempFile( s );

var fs = new FileStream( _resolvedTempFilePath, FileMode.Open );
return new MagicTempFileStreamManagerAndDeleterFuckery( fs );
}

private void ImportV1ModPack( FileInfo modPackFile )
Expand All @@ -59,8 +109,7 @@ private void ImportV1ModPack( FileInfo modPackFile )
};

// Open the mod data file from the modpack as a SqPackStream
var mpd = extractedModPack.GetEntry( "TTMPD.mpd" );
var modData = GetSqPackStreamFromZipEntry( extractedModPack, mpd );
using var modData = GetMagicSqPackDeleterStream( extractedModPack, "TTMPD.mpd" );

var newModFolder = new DirectoryInfo(
Path.Combine( _outDirectory.FullName,
Expand Down Expand Up @@ -115,8 +164,7 @@ private void ImportSimpleV2ModPack( ZipFile extractedModPack )
};

// Open the mod data file from the modpack as a SqPackStream
var mpd = extractedModPack.GetEntry( "TTMPD.mpd" );
var modData = GetSqPackStreamFromZipEntry( extractedModPack, mpd );
using var modData = GetMagicSqPackDeleterStream( extractedModPack, "TTMPD.mpd" );

var newModFolder = new DirectoryInfo( Path.Combine( _outDirectory.FullName,
Path.GetFileNameWithoutExtension( modList.Name ) ) );
Expand All @@ -142,12 +190,12 @@ private void ImportExtendedV2ModPack( ZipFile extractedModPack )
Name = modList.Name,
Description = string.IsNullOrEmpty( modList.Description )
? "Mod imported from TexTools mod pack"
: modList.Description
: modList.Description,
Version = modList.Version
};

// Open the mod data file from the modpack as a SqPackStream
var mpd = extractedModPack.GetEntry( "TTMPD.mpd" );
var modData = GetSqPackStreamFromZipEntry( extractedModPack, mpd );
using var modData = GetMagicSqPackDeleterStream( extractedModPack, "TTMPD.mpd" );

var newModFolder = new DirectoryInfo(
Path.Combine( _outDirectory.FullName,
Expand Down Expand Up @@ -187,13 +235,24 @@ private void ImportMetaModPack( FileInfo file )

private void ExtractSimpleModList( DirectoryInfo outDirectory, IEnumerable< SimpleMod > mods, SqPackStream dataStream )
{
State = ImporterState.ExtractingModFiles;

// haha allocation go brr
var wtf = mods.ToList();

TotalProgress = wtf.LongCount();

// Extract each SimpleMod into the new mod folder
foreach( var simpleMod in mods )
foreach( var simpleMod in wtf )
{
if( simpleMod == null )
{
// do we increment here too???? can this even happen?????
continue;

}

ExtractMod( outDirectory, simpleMod, dataStream );
CurrentProgress++;
}
}

Expand Down Expand Up @@ -228,10 +287,5 @@ private static string GetStringFromZipEntry( ZipFile file, ZipEntry entry, Encod
s.CopyTo( ms );
return encoding.GetString( ms.ToArray() );
}

private static SqPackStream GetSqPackStreamFromZipEntry( ZipFile file, ZipEntry entry )
{
return new( GetStreamFromZipEntry( file, entry ) );
}
}
}
4 changes: 4 additions & 0 deletions Penumbra/Models/ModMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public class ModMeta
public string Name { get; set; }
public string Author { get; set; }
public string Description { get; set; }

public string Version { get; set; }

public string Website { get; set; }

public Dictionary< string, string > FileSwaps { get; } = new();
}
Expand Down
10 changes: 5 additions & 5 deletions Penumbra/Mods/ModCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ModCollection
public List< ModInfo > ModSettings { get; set; }
public ResourceMod[] EnabledMods { get; set; }


public ModCollection( DirectoryInfo basePath )
{
_basePath = basePath;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void Save()
public void ReorderMod( ModInfo info, bool up )
{
// todo: certified fucked tier

var prio = info.Priority;
var swapPrio = up ? prio + 1 : prio - 1;
var swapMeta = ModSettings.FirstOrDefault( x => x.Priority == swapPrio );
Expand All @@ -126,19 +127,18 @@ public void ReorderMod( ModInfo info, bool up )
{
return;
}

info.Priority = swapPrio;
swapMeta.Priority = prio;

// reorder mods list
ModSettings = ModSettings.OrderBy( x => x.Priority ).ToList();
EnabledMods = GetOrderedAndEnabledModList().ToArray();

// save new prios
Save();
}




public ModInfo FindModSettings( string name )
{
Expand Down
53 changes: 50 additions & 3 deletions Penumbra/Mods/ModManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using Penumbra.Models;

namespace Penumbra.Mods
{
public class ModManager
public class ModManager : IDisposable
{
public readonly Dictionary< string, FileInfo > ResolvedFiles = new();
public readonly Dictionary< string, string > SwappedFiles = new();
Expand All @@ -23,6 +24,33 @@ public void DiscoverMods()
DiscoverMods( _basePath );
}

// private void FileSystemWatcherOnChanged( object sender, FileSystemEventArgs e )
// {
// #if DEBUG
// PluginLog.Verbose( "file changed: {FullPath}", e.FullPath );
// #endif
//
// if( _plugin.ImportInProgress )
// {
// return;
// }
//
// if( _plugin.Configuration.DisableFileSystemNotifications )
// {
// return;
// }
//
// var file = e.FullPath;
//
// if( !ResolvedFiles.Any( x => x.Value.FullName == file ) )
// {
// return;
// }
//
// PluginLog.Log( "a loaded file has been modified - file: {FullPath}", file );
// _plugin.GameUtils.ReloadPlayerResources();
// }

public void DiscoverMods( string basePath )
{
DiscoverMods( new DirectoryInfo( basePath ) );
Expand All @@ -43,7 +71,21 @@ public void DiscoverMods( DirectoryInfo basePath )

_basePath = basePath;

ResolvedFiles.Clear();
// haha spaghet
// _fileSystemWatcher?.Dispose();
// _fileSystemWatcher = new FileSystemWatcher( _basePath.FullName )
// {
// NotifyFilter = NotifyFilters.LastWrite |
// NotifyFilters.FileName |
// NotifyFilters.DirectoryName,
// IncludeSubdirectories = true,
// EnableRaisingEvents = true
// };
//
// _fileSystemWatcher.Changed += FileSystemWatcherOnChanged;
// _fileSystemWatcher.Created += FileSystemWatcherOnChanged;
// _fileSystemWatcher.Deleted += FileSystemWatcherOnChanged;
// _fileSystemWatcher.Renamed += FileSystemWatcherOnChanged;

Mods = new ModCollection( basePath );
Mods.Load();
Expand All @@ -62,7 +104,7 @@ public void CalculateEffectiveFileList()
foreach( var mod in Mods.GetOrderedAndEnabledModList() )
{
mod.FileConflicts?.Clear();

// fixup path
var baseDir = mod.ModBasePath.FullName;

Expand Down Expand Up @@ -138,5 +180,10 @@ public string ResolveSwappedOrReplacementFilePath( string gameResourcePath )

return GetCandidateForGameFile( gameResourcePath )?.FullName ?? GetSwappedFilePath( gameResourcePath );
}

public void Dispose()
{
// _fileSystemWatcher?.Dispose();
}
}
}
3 changes: 2 additions & 1 deletion Penumbra/Penumbra.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"RepoUrl": "https://github.com/xivdev/Penumbra",
"ApplicableVersion": "any",
"Tags": [ "modding" ],
"DalamudApiLevel": 69420
"DalamudApiLevel": 69420,
"LoadPriority": 69420
}
Loading

0 comments on commit 8adeab1

Please sign in to comment.