Giter Club home page Giter Club logo

cpk.net's Introduction

About Cpk.Net

This is an implementation of the CPack file reader in C# using .NET Standard 2.1. CPack(.cpk) is a file format used to store game assets in the game Pal3(仙剑奇侠传三) and Pal3a(仙剑奇侠传3外传-问情篇) created by Softstar Technology (Shanghai) Co., Ltd.

How to build Cpk.Net?

dotnet build

Code Example

// Example to load and open one file stored inside a CPK archive using Cpk.Net

using Cpk.Net;

const string CpkPath = "...";
const string VirtualFilePath = "..."; // Virtualized/relative file path within CPK archive

var cpk = new CpkArchive(CpkPath);
await cpk.LoadAsync();
await using var stream = cpk.Open(VirtualFilePath, out uint size, out bool isCompressed);
...
// Example to load and upack all files stored inside a CPK archive using Cpk.Net

using Cpk.Net;

const string CpkPath = "...";
const string OutputFolderPath = "...";

var cpk = new CpkArchive(CpkPath);
await cpk.LoadAsync(loadIntoMemory: true);
var rootNodes = await cpk.GetRootEntriesAsync();

Unpack(rootNodes, OutputFolderPath);

void Unpack(IList<CpkEntry> nodes, string rootPath)
{
    foreach (var node in nodes)
    {
        // You need to replace CPK directory separator char with
        // your current system directory separator char.
        // So this code can run anywhere.
        var relativePath = node.VirtualPath.Replace(
            CpkConstants.CpkVirtualDirectorySeparatorChar, Path.DirectorySeparatorChar);

        if (node.IsDirectory)
        {
            new DirectoryInfo(rootPath + relativePath).Create();
            Unpack(node.Children, rootPath);
        }
        else
        {
            using var readStream = cpk.Open(node.VirtualPath, out var size, out var isCompressed);
            using var writeStream = new FileStream(rootPath + relativePath, FileMode.Create, FileAccess.Write);
            CopyStream(readStream, writeStream, (int)size, isCompressed ? size : 32768);
            Console.WriteLine($"{node.VirtualPath} unpacked.");
        }
    }
}

static void CopyStream(Stream input, Stream output, int length, uint bufferSize)
{
    byte[] buffer = new byte[bufferSize];
    int read;
    while ((read = input.Read(buffer, 0, Math.Min(buffer.Length, length))) > 0)
    {
        output.Write(buffer, 0, read);
        length -= read;
    }
}

License

MIT License

Copyright (c) 2022 Jiaqi Liu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cpk.net's People

Contributors

0x7c13 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cpk.net's Issues

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.