Giter Site home page Giter Site logo

squeek502 / d2itemreader Goto Github PK

View Code? Open in Web Editor NEW
39.0 6.0 2.0 2.09 MB

C library for parsing items in Diablo II character/stash files

Home Page: https://squeek502.github.io/d2itemreader/

License: The Unlicense

CMake 4.08% Batchfile 0.52% C 93.78% C++ 1.62%
d2s diablo-ii diablo

d2itemreader's Introduction

d2itemreader

Build status Build status

work in progress, everything is subject to change

d2itemreader is a C library for parsing Diablo II character/stash files (.d2s, .d2x, and .sss) and retrieving data about the items contained inside them. It also tries to avoid any assumptions about the game version or game data, so that it can work with modded files (provided the library is initialized with the relevant modded .txt files on startup).

Usage

Most API functions in d2itemreader.h work in the following way:

  • There is a <struct>_parse function that takes a pointer to a struct and returns a d2err enum.
    • If the function returns D2ERR_OK, then the function succeeded and the struct will need to be cleaned up using the corresponding _destroy function.
    • If the function returns anything other than D2ERR_OK, then the _destroy function does not need to be called; any allocated memory is cleaned up by the _parse or _init function before it returns an error.
    • The out_bytesRead parameter will always be set regardless of the result of the _parse function. On failure, it will contain the number of bytes read before the error occured.

On program startup, you will need to initialize a d2gamedata struct with the data from some of Diablo II's .txt files found in its .mpq archives. For convenience, d2itemreader bundles the relevant data from the latest .txt files (1.14d), which can be loaded by calling:

d2gamedata gameData;
d2err err = d2gamedata_init_default(&gameData);

If the d2gamedata_init function returns D2ERR_OK, the following function should be called on shutdown (or when you're done using the d2itemreader library):

d2gamedata_destroy(&gameData);

After the d2gamedata_init function is called, you can parse files like so:

const char *filename = "path/to/file";

// determine the filetype if it is not known in advance
enum d2filetype filetype = d2filetype_of_file(filename);

if (filetype != D2FILETYPE_D2_CHARACTER)
{
	fprintf(stderr, "File is not a d2 character file: %s\n", filename);
	return;
}

size_t bytesRead;
d2char character;
d2err err = d2char_parse_file(filename, &character, &gameData, &bytesRead);
if (err != D2ERR_OK)
{
	fprintf(stderr, "Failed to parse %s: %s at byte 0x%zx\n", filename, d2err_str(err), bytesRead);
	// don't need to call d2char_destroy, the memory is cleaned up when _parse returns an error
}
else
{
	// do something with the character data
	int numUniques = 0;

	for (int i=0; i<character.items.count; i++)
	{
		d2item* item = &character.items.items[i];
		if (item->rarity == D2RARITY_UNIQUE)
		{
			numUniques++;
		}
	}

	printf("Number of unique items in %s: %d", filename, numUniques);

	// clean up the memory allocated when parsing the character file
	d2char_destroy(&character);
}

Bindings

Acknowledgements

  • nokka/d2s - much of the d2s parsing of d2itemreader is ported from nokka/d2s

d2itemreader's People

Contributors

squeek502 avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

d2itemreader's Issues

Corrupted .sss file please help

Hi @squeek502

Please could you help me. I couldn't find another way to contact you so i'm opening an issue.

I'm looking for a way of restoring my plugy shared stash file. It got corrupted when plugy didnt save properly.

I'm trying to read my .sss file and see if i can restore it. Is this possible? Do you know how I could do it?

I'm busy looking at the plugy source code and found your repo which looks promising on how to read a .sss file.

When i try to use the corrupted .sss file the game gives error 9 "Unable to enter game. Bad header"

When I only try to use the bad character i get "Unable to enter game. Bad character version"

Please find the attached .sss file. I haven't figured out how to use the d2itemreader yet.
save_files.ZIP

// Harry

Item Y coordinate -- possible value range conflict with stash page size

the D2 item format as described here:
https://squeek502.github.io/d2itemreader/formats/d2.html#item-list-data-format

reserves a 4 bit wide value (possible values range from 0 to 15) for the X coordinate, and a 3 bit wide value (possible values range from 0 to 7) for the Y coordinate. This representation is not sufficient to place the item in the PlugY stash, which has a page size of 10 by 10.

Conveniently, after the Y coordinate is a bit labeled unknown. After checking my limited set of test data, it seems to me that PlugY uses this unknown bit to extend the Y coordinate to a 4 bit field.

It seems tempting to assume that the item format reserves 4 bit for each X and Y coordinate, or are there test files that contradict this assumption?

Handle set bonuses properly

See nokka/d2s#5

The set bits correspond with how many items of that set need to be worn to get that bonus, and also with the values of the aprop fields in SetItems.txt.

If the bits are numbered 54321:

  • If bit 1 is set, then it requires >= 2 items worn
  • If bit 2 is set, then it requires >= 3 items worn
  • If bit 3 is set, then it requires >= 4 items worn
  • If bit 4 is set, then it requires >= 5 items worn
  • If bit 5 is set, then it requires >= 6 items worn

Examples:

  • M'avina's Belt

    • Set bonuses flag: 4 (00100)
    • SetItems.txt only has aprop3 field set
    • Only gets the bonus when >= 4 items are worn
  • IK Armor

    • Set bonuses flag: 31 (11111)
    • SetItems.txt has aprop1-aprop5 fields set
    • Get a new bonus each additional item that is worn
  • Trang's Armor

    • Set bonuses flag: 10 (01010)
    • SetItems.txt has aprop2 and aprop4 fields set
    • Only gets a new bonus when >= 3 items are worn and then another at >= 5 worn

Handle negative "Save Add" column in ItemStatCost.txt properly

For example, "Hit Causes Monster to Flee" (id 112) has a "Save Add" column with value -1. Need to check if this is truly a negative value or if -1 denotes something else. Either way, ItemStatCost is parsed by d2itemreader assuming unsigned integers, so there is a bug here somewhere (-1 becomes 65535).

Test against PlugY 14.x

AFAIK this is the only relevant change:

  • D2_MAX_STASH_PAGE_NAME_STRLEN got upped to 20 (from 15)

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.