GithubHelp home page GithubHelp logo

maikelchan / afslib Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 1.0 140 KB

Library to manipulate AFS files

Home Page: https://pacochan.net/software/afs-packer/

License: MIT License

C# 100.00%
games packer unpacker romhacking afs afs-archive criware

afslib's Introduction

AFSLib

GitHub release (latest SemVer) Nuget GitHub commits since latest release (by SemVer) GitHub

AFSLib is a library that can extract, create and manipulate AFS files. The AFS format is used in many games from companies like Sega. Even though it's a simple format, there are lots of quirks and edge cases in many games that require implementing specific fixes or workarounds for the library to work with them. If you encounter any issue with a specific game, don't hesitate to report it.

For a usage example, you can check AFSPacker, a simple command line tool that uses AFSLib to extract and create AFS files.

Usage examples

In order to use this examples, make sure you are using AFSLib.

using AFSLib;

Create an AFS archive from scratch with some files

// Create a new AFS object.

using (AFS afs = new AFS())
{
    // Add two files. The first parameter is the path to the file.
    // By default, an entry will be created with the same name as the file.
    // But you can also set a different entry name with the second parameter.

    afs.AddEntryFromFile(@"C:\Data\Files\MyTextFile.txt");
    afs.AddEntryFromFile(@"C:\Data\Files\MyImageFile.png", "Title.png");

    // Finally save the AFS archive to the specified file.

    afs.SaveToFile(@"C:\Data\MyArchive.afs");
}

Create an AFS archive out of all the files inside a directory

// Create a new AFS object.

using (AFS afs = new AFS())
{
    // Add all the files located in the directory C:\Data\Files.

    afs.AddEntriesFromDirectory(@"C:\Data\Files");

    // Finally save the AFS archive to the specified file.

    afs.SaveToFile(@"C:\Data\MyArchive.afs");
}

Extract all the entries from an existing AFS archive

// Create an AFS object out of an existing AFS archive.

using (AFS afs = new AFS(@"C:\Data\MyArchive.afs"))
{
    // Extract all the entries to the specified directory.

    afs.ExtractAllEntriesToDirectory(@"C:\Data\Files");
}

Change some entries properties and save them in another AFS archive

// Create an AFS object out of an existing AFS archive.

using (AFS afs = new AFS(@"C:\Data\MyArchive.afs"))
{
    // To change some properties, first we need to be sure that we
    // are trying to change an entry with data, and not a null entry.
    // So cast to DataEntry, which is the parent class of all entries
    // that can contain data.

    DataEntry dataEntry = afs.Entries[0] as DataEntry;
    if (dataEntry != null)
    {
        // It seems that it's indeed an entry with data,
        // so let's change some stuff.

        dataEntry.Name = "NewFileName.txt";
        dataEntry.LastWriteTime = DateTime.Now;
    }

    // Save again with a different name.

    afs.SaveToFile(@"C:\Data\MyNewArchive.afs");
}

Advanced AFS creation

There are many variants of the AFS format found in many games. Some of those games may require to have a specific variant of AFS archive. There are several modifiable properties that allow to customize the archive so it matches what the game needs. To know what values are needed, read them from one of the AFS archives found in the game.

// Some variables to store the properties

HeaderMagicType header;
AttributesInfoType attributesInfo;
uint entryBlockAlignment;

// Create an AFS object out of an existing AFS archive.

using (AFS afs = new AFS(@"C:\Data\MyArchive.afs"))
{
    // Read and store some properties

    header = afs.HeaderMagicType;
    attributesInfo = afs.AttributesInfoType;
    entryBlockAlignment = afs.EntryBlockAlignment;
}

// Create a new AFS object.

using (AFS afs = new AFS())
{
    // Set the same properties to the new file

    afs.HeaderMagicType = header;
    afs.AttributesInfoType = attributesInfo;
    afs.EntryBlockAlignment = entryBlockAlignment;

    // Add some files

    afs.AddEntriesFromDirectory(@"C:\Data\Files");

    // Finally save the AFS archive to the specified file.

    afs.SaveToFile(@"C:\Data\MyArchive.afs");
}

Get and print progress information

// Create an AFS object out of an existing AFS archive.

using (AFS afs = new AFS(@"C:\Data\MyArchive.afs"))
{
    // Subscribe to the event so the extraction process will send info about its progress
    // and can be printed with the method Afs_NotifyProgress.

    afs.NotifyProgress += Afs_NotifyProgress;

    // Extract all the entries to the specified directory.

    afs.ExtractAllEntriesToDirectory(@"C:\Data\Files");
}

static void Afs_NotifyProgress(NotificationType type, string message)
{
    Console.WriteLine($"[{type}] - {message}");
}

afslib's People

Contributors

maikelchan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.