Skip to content

Commit

Permalink
Merge pull request #242 from onepiecefreak3/master
Browse files Browse the repository at this point in the history
Added AMB Load/Save;
  • Loading branch information
onepiecefreak3 authored Aug 10, 2017
2 parents 733ee8c + 6b2c25f commit e333299
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/archive/archive_amb/AMB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Kuriimu.Compression;
using Kuriimu.Kontract;
using Kuriimu.IO;

namespace archive_amb
{
public class AMB
{
public List<AMBFileInfo> Files = new List<AMBFileInfo>();
Stream _stream = null;

public Header header;
public List<FileEntry> entries = new List<FileEntry>();

public AMB(Stream input)
{
_stream = input;
using (BinaryReaderX br = new BinaryReaderX(input, true, ByteOrder.BigEndian))
{
//Header
header = br.ReadStruct<Header>();

//FileEntries
entries = br.ReadMultiple<FileEntry>((int)header.fileCount);

//Files
var count = 0;
foreach (var entry in entries)
Files.Add(new AMBFileInfo
{
State = ArchiveFileState.Archived,
FileName = $"{count++:00000000}.bin",
FileData = new SubStream(br.BaseStream, entry.offset, entry.size)
});
}
}

public void Save(Stream output)
{
using (var bw = new BinaryWriterX(output, ByteOrder.BigEndian))
{
//Header
bw.WriteStruct(header);

//FileEntries
var count = 0;
var dataOffset = header.dataOffset;
foreach (var file in Files)
{
entries[count].offset = dataOffset;
entries[count++].size = (uint)file.FileSize;

dataOffset = (uint)((dataOffset + (uint)file.FileSize + 0xf) & ~0xf);
}

foreach (var entry in entries)
{
bw.WriteStruct(entry);
}

//Files
bw.BaseStream.Position = header.dataOffset;
foreach (var file in Files)
{
file.FileData.CopyTo(bw.BaseStream);
bw.WriteAlignment();
}
}
}

public void Close()
{
_stream?.Close();
_stream = null;
}
}
}
92 changes: 92 additions & 0 deletions src/archive/archive_amb/AmbManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Kuriimu.Kontract;
using Kuriimu.IO;

namespace archive_amb
{
public class AmbManager : IArchiveManager
{
private AMB _amb = null;

#region Properties

// Information
public string Name => "AMB";
public string Description => "Whatever AMB means";
public string Extension => "*.amb";
public string About => "This is the AMB archive manager for Karameru.";

// Feature Support
public bool ArchiveHasExtendedProperties => false;
public bool CanAddFiles => false;
public bool CanRenameFiles => false;
public bool CanReplaceFiles => true;
public bool CanDeleteFiles => false;
public bool CanSave => true;

public FileInfo FileInfo { get; set; }

#endregion

public bool Identify(string filename)
{
using (var br = new BinaryReaderX(File.OpenRead(filename)))
{
if (br.BaseStream.Length < 4) return false;
var magic = br.ReadString(4);
return magic == "#AMB";
}
}

public void Load(string filename)
{
FileInfo = new FileInfo(filename);

if (FileInfo.Exists)
_amb = new AMB(FileInfo.OpenRead());
}

public void Save(string filename = "")
{
if (!string.IsNullOrEmpty(filename))
FileInfo = new FileInfo(filename);

// Save As...
if (!string.IsNullOrEmpty(filename))
{
_amb.Save(FileInfo.Create());
_amb.Close();
}
else
{
// Create the temp file
_amb.Save(File.Create(FileInfo.FullName + ".tmp"));
_amb.Close();
// Delete the original
FileInfo.Delete();
// Rename the temporary file
File.Move(FileInfo.FullName + ".tmp", FileInfo.FullName);
}

// Reload the new file to make sure everything is in order
Load(FileInfo.FullName);
}

public void Unload()
{
_amb?.Close();
}

// Files
public IEnumerable<ArchiveFileInfo> Files => _amb.Files;

public bool AddFile(ArchiveFileInfo afi) => false;

public bool DeleteFile(ArchiveFileInfo afi) => false;

// Features
public bool ShowProperties(Icon icon) => false;
}
}
32 changes: 32 additions & 0 deletions src/archive/archive_amb/AmbSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Runtime.InteropServices;
using Kuriimu.Kontract;

namespace archive_amb
{
public class AMBFileInfo : ArchiveFileInfo
{

}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class Header
{
public Magic magic;
public uint headerSize;
public int zero0;
public uint dirCount; //??
public uint fileCount;
public uint fileEntryOffset;
public uint dataOffset;
public uint zero1;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class FileEntry
{
public uint offset;
public uint size;
public uint id;
public uint zero0;
}
}
36 changes: 36 additions & 0 deletions src/archive/archive_amb/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("archive_amb")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("archive_amb")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("65b03a2b-bcf5-494e-a87c-dd4ac2d4f286")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
41 changes: 41 additions & 0 deletions src/archive/archive_amb/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/archive/archive_amb/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="archive_amb.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="PluginName" Type="System.String" Scope="Application">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
15 changes: 15 additions & 0 deletions src/archive/archive_amb/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="archive_amb.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<archive_amb.Properties.Settings>
<setting name="PluginName" serializeAs="String">
<value />
</setting>
</archive_amb.Properties.Settings>
</applicationSettings>
</configuration>
68 changes: 68 additions & 0 deletions src/archive/archive_amb/archive_amb.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{65B03A2B-BCF5-494E-A87C-DD4AC2D4F286}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>archive_amb</RootNamespace>
<AssemblyName>archive_amb</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Kuriimu\bin\Debug\plugins\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Kuriimu\bin\Release\plugins\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="AMB.cs" />
<Compile Include="AmbManager.cs" />
<Compile Include="AmbSupport.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Kontract\Kontract.csproj">
<Project>{51e474f7-1497-4c30-bc18-c357c884b8b2}</Project>
<Name>KuriimuContract</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit e333299

Please sign in to comment.