Interface ModAPI

This is the interface of the global "D2RMM" variable provided to mods at runtime.

Example

const version = D2RMM.getVersion();
interface ModAPI {
    copyFile: ((src, dst, overwrite?) => void);
    error: ((message) => void);
    getConfigJSON: (() => string);
    getFullVersion: (() => [number, number, number]);
    getModList: (() => {
        config: Readonly<{
            [key: string]: ModConfigSingleValue;
        }>;
        id: string;
        installed: boolean;
        name: string;
        version: undefined | string;
    }[]);
    getNextStringID: (() => number);
    getVersion: (() => number);
    readJson: ((filePath) => JSONData);
    readSaveFile: ((filePath) => null | number[]);
    readTsv: ((filePath) => TSVData);
    readTxt: ((filePath) => string);
    writeJson: ((filePath, data) => void);
    writeSaveFile: ((filePath, data) => void);
    writeTsv: ((filePath, data) => void);
    writeTxt: ((filePath, data) => void);
}

Properties

copyFile: ((src, dst, overwrite?) => void)

Copies a file or directory from the mod directory to the data directory. This is primarily used for including non-mergeable assets like sprites in your mod.

Type declaration

    • (src, dst, overwrite?): void
    • Parameters

      • src: string

        The path of the file or directory to copy, relative to the mod directory.

      • dst: string

        The path of the file or directory to copy to, relative to the data directory.

      • Optional overwrite: boolean

        Whether to overwrite any conflicts.

      Returns void

Note

While you can use this API to provide whole new versions of TSV/JSON game files to the game, this is an anti-pattern and will dramatically reduce your mod's compatibility with other mods. Don't do it. Use the read* and write* APIs instead.

Example

// copy new sprites to the output directory
D2RMM.copyFile(
'hd', // <mod folder>\hd
'hd', // <diablo 2 folder>\mods\D2RMM\D2RMM.mpq\data\hd
true // overwrite any conflicts
);
error: ((message) => void)

Shows an error message to the user.

Type declaration

    • (message): void
    • Parameters

      • message: string | Error

        The message to show.

      Returns void

Deprecated

Use console.error() or throw new Error() instead.

Example

D2RMM.error('Something went wrong!');
D2RMM.error(new Error('Something went wrong!'));
getConfigJSON: (() => string)

Returns the configuration of the mod as a JSON string.

Type declaration

    • (): string
    • Returns string

Note

This is an internal API. You should reference the JSON parsed version of this value using the global config object instead.

Example

const config = D2RMM.getConfigJSON();
console.log('isFooEnabled = ' + JSON.parse(config).isFooEnabled);

Returns

The configuration of the mod.

getFullVersion: (() => [number, number, number])

Returns the full version of D2RMM composed of the major, minor, and patch versions as an array of integers.

Type declaration

    • (): [number, number, number]
    • Returns [number, number, number]

Note

Mods should not need to rely on the patch version.

Example

const [major, minor, patch] = D2RMM.getFullVersion();

Returns

The version including the major, minor, and patch numbers.

getModList: (() => {
    config: Readonly<{
        [key: string]: ModConfigSingleValue;
    }>;
    id: string;
    installed: boolean;
    name: string;
    version: undefined | string;
}[])

Returns the list of mods being installed.

Type declaration

    • (): {
          config: Readonly<{
              [key: string]: ModConfigSingleValue;
          }>;
          id: string;
          installed: boolean;
          name: string;
          version: undefined | string;
      }[]
    • Returns {
          config: Readonly<{
              [key: string]: ModConfigSingleValue;
          }>;
          id: string;
          installed: boolean;
          name: string;
          version: undefined | string;
      }[]

Example

const modList = D2RMM.getModList();
modList.forEach(mod => {
console.log(
`mod ${mod.name} (${mod.id})`,
`v${mod.version}`,
`is ${mod.installed ? 'already installed' : 'not yet installed'}`,
`with config ${JSON.stringify(mod.config)}`,
);
});

Returns

The list of installed mods.

getNextStringID: (() => number)

Produces the next valid string ID to use as an identifier in D2R's data files. The ID is read from next_string_id.txt, and then incremented within that file.

Type declaration

    • (): number
    • Returns number

Returns

The next valid string ID.

getVersion: (() => number)

Returns the version of D2RMM composed of the major and minor versions as a float.

Type declaration

    • (): number
    • Returns number

Note

You can use this API to check if the installed version of D2RMM is compatible with the APIs that your mod is using.

Example

const version = D2RMM.getVersion(); // 1.5

Returns

The version including the major and the minor number.

readJson: ((filePath) => JSONData)

Reads a JSON D2R file.

Type declaration

    • (filePath): JSONData
    • Parameters

      • filePath: string

        The path of the file to read, relative to the data directory.

      Returns JSONData

Note

D2R's JSON files don't follow the standard JSON spec. This method will ignore any comments, whitespace, and various invalid properties (for example, duplicate keys), that D2R might use.

Note

The file is either read from D2R game files as specified in D2RMM's config, or is the result of previously installed mods already operating on this file.

Example

const profileHD = D2RMM.readJson('global\\ui\\layouts\\_profilehd.json');
profileHD.FontColorRed.r; // 252

Returns

The parsed JSON data.

readSaveFile: ((filePath) => null | number[])

Reads a save file from the saves directory as binary. The result is an array of bytes.

Type declaration

    • (filePath): null | number[]
    • Parameters

      • filePath: string

        The path of the save file to read, relative to the saves directory.

      Returns null | number[]

Example

const stashData = D2RMM.readSaveFile('SharedStashSoftCoreV2.d2i');
console.log('Save file size: ' + stashData.length);

Returns

The binary data of the save file.

readTsv: ((filePath) => TSVData)

Reads a TSV (tab separated values in a .txt) D2R file. This is a classic data format used by D2.

Type declaration

    • (filePath): TSVData
    • Parameters

      • filePath: string

        The path of the file to read, relative to the data directory.

      Returns TSVData

Note

The last column in the file will probably have a \r at the end of its name.

Note

The file is either read from D2R game files as specified in D2RMM's config, or is the result of previously installed mods already operating on this file.

Example

const treasureclassex = D2RMM.readTsv('global\\excel\\treasureclassex.txt');
console.log('There are ' + treasureclassex.rows.length + ' treasure classes!');
console.log('Each treasure class has ' + treasureclassex.headers.length + ' properties!');

Returns

The parsed TSV data.

readTxt: ((filePath) => string)

Reads a plain text D2R file.

Type declaration

    • (filePath): string
    • Parameters

      • filePath: string

        The path of the file to read, relative to the data directory.

      Returns string

Note

The file is either read from D2R game files as specified in D2RMM's config, or is the result of previously installed mods already operating on this file.

Example

const nextStringIDRaw = D2RMM.readJson('local\\next_string_id.txt');
let nextStringID = nextStringIDRaw.match(/[0-9]+/)[0]; // next valid string id

Returns

The raw text data.

writeJson: ((filePath, data) => void)

Writes a JSON D2R file.

Type declaration

    • (filePath, data): void
    • Parameters

      • filePath: string

        The path of the file to write, relative to the data directory.

      • data: JSONData

        The JSON data to write.

      Returns void

Example

// change red colored text to bright green!
const profileHD = D2RMM.readJson('global\\ui\\layouts\\_profilehd.json');
profileHD.FontColorRed = {r: 0, b: 0, g: 255, a: 255};
D2RMM.writeJson('global\\ui\\layouts\\_profilehd.json', profileHD);
writeSaveFile: ((filePath, data) => void)

Writes a save file to the saves directory as binary.

Type declaration

    • (filePath, data): void
    • Parameters

      • filePath: string

        The path of the save file to write, relative to the saves directory.

      • data: number[]

        The binary data of the save file to write as an array of bytes.

      Returns void

Note

It's highly recommended to write a backup of any save file you are modifying because save files can be corrupted if written incorrectly.

Example

const stashData = D2RMM.readSaveFile('SharedStashSoftCoreV2.d2i');
D2RMM.writeSaveFile('SharedStashSoftCoreV2.d2i', stashData.concat(EXTRA_STASH_TAB));
writeTsv: ((filePath, data) => void)

Writes a TSV (tab separated values in a .txt) D2R file. This is a classic data format used by D2.

Type declaration

    • (filePath, data): void
    • Parameters

      • filePath: string

        The path of the file to write, relative to the data directory.

      • data: TSVData

        The TSV data to write.

      Returns void

Example

const treasureclassex = D2RMM.readTsv('global\\excel\\treasureclassex.txt');
treasureclassex.rows.forEach(row => {
// D2R TSV files sometimes have blank rows
if (row['Treasure Class'] !== '') {
row.NoDrop = 1;
}
});
D2RMM.writeTsv('global\\excel\\treasureclassex.txt', treasureclassex);
writeTxt: ((filePath, data) => void)

Writes a plain text D2R file.

Type declaration

    • (filePath, data): void
    • Parameters

      • filePath: string

        The path of the file to write, relative to the data directory.

      • data: string

        The raw text data to write.

      Returns void

Example

const nextStringIDRaw = D2RMM.readTxt('local\\next_string_id.txt');
let nextStringID = nextStringIDRaw.match(/[0-9]+/)[0]; // next valid string id
nextStringID ++;
nextStringIDRaw.replace(/[0-9]+/, nextStringID);
D2RMM.writeTxt('local\\next_string_id.txt', nextStringIDRaw);