Skip to content

Commit

Permalink
v1.12.3.0: Allow disambiguated package IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
ruipin committed Feb 1, 2022
1 parent 62afafe commit f93c00c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.12.3.0 (2022-02-01)

- All API functions now accept disambiguation prefixes as part of their `package_id` parameter, i.e. `module:foobar`, `system:foobar`, or `world:foobar`.

# 1.12.2.0 (2022-01-30)

- Allow integer enum values to be passed to `get` as a string, so that the caller does not need to cast explicitly.
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lib-wrapper",
"title": "libWrapper",
"description": "Library for wrapping core Foundry VTT methods, meant to improve compatibility between packages that wrap the same methods.",
"version": "1.12.2.0",
"version": "1.12.3.0",
"author": "Rui Pinheiro",
"esmodules": ["src/index.js"],
"styles": ["dist/lib-wrapper.css"],
Expand Down
17 changes: 13 additions & 4 deletions src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,30 +258,39 @@ export function _unwrap_all() {
WRAPPERS.clear();
}


function _get_package_info(package_id) {
// Auto-detect package info, initially
let package_info = new PackageInfo();

if(!PackageInfo.is_valid_id(package_id))
// Sanity check user provided ID
if(!PackageInfo.is_valid_key_or_id(package_id))
throw new ERRORS.package('Parameter \'package_id\' is invalid.', package_info);

// Parse user provided ID into a PackageInfo object
const user_package_info = new PackageInfo(package_id);

// If we were able to auto-detect the package, validate user provided info against automatically detected info
if(package_info.exists) {
if(package_id != package_info.id)
if(!package_info.equals(user_package_info))
throw new ERRORS.package(`${package_info.type_plus_id_capitalized} is not allowed to call libWrapper with package_id='${package_id}'.`, package_info);
}
// Otherwise, just assume what the user provided is correct
else {
package_info = new PackageInfo(package_id);
package_info = user_package_info;
}

// Sanity Check: Must not allow registering wrappers as lib-wrapper
if(package_id == PACKAGE_ID) {
if(!allow_libwrapper_registrations)
throw new ERRORS.package(`Not allowed to call libWrapper with package_id='${package_id}'.`, package_info);
}
// Sanity Check: Package must exist (single exception is lib-wrapper, since we register wrappers before 'init')
else {
if(!package_info.exists && game.modules?.size)
throw new ERRORS.package(`Package '${package_id}' is not a valid package.`, package_info);
}

// Done
return package_info;
}

Expand Down
2 changes: 1 addition & 1 deletion src/shared
Submodule shared updated 1 files
+37 −16 package_info.js
38 changes: 26 additions & 12 deletions src/ui/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ import { Log } from '../shared/log.js';
// Map of currently loaded priorities
export const PRIORITIES = new Map();


function is_valid_priority_key_data(key, data) {
if(!PackageInfo.is_valid_key_or_id(key))
return false;

if(!data)
return false;

if(typeof data.id !== 'string' || typeof data.title !== 'string' || typeof data.index !== 'number')
return false;

return true;
}

export const load_priorities = function(value=null) {
// Create existing priorities
PRIORITIES.clear();
Expand All @@ -34,10 +48,10 @@ export const load_priorities = function(value=null) {
Object.entries(current).forEach(entry => {
let [key, data] = entry;

// Handle legacy format, if found
if(!data.id) {
data = new PackageInfo(key, PACKAGE_TYPES.MODULE);
key = data.key;
// Check key/data validity
if(!is_valid_priority_key_data(key, data)) {
Log.warn$?.(`Ignoring '${key}' entry while loading module priorities due to invalid format.`)
return;
}

// Add to priorities dictionary
Expand Down Expand Up @@ -315,10 +329,10 @@ export class LibWrapperSettings extends FormApplication {
Object.entries(cfg_prioritized).forEach((entry) => {
let [key, data] = entry;

// Handle legacy format, if found
if(!data.id) {
data = new PackageInfo(key, PACKAGE_TYPES.MODULE);
key = data.key;
// Check key/data validity
if(!is_valid_priority_key_data(key, data)) {
Log.warn$?.(`Ignoring '${key}' entry while loading module priorities due to invalid format.`)
return;
}

// Push data
Expand All @@ -339,10 +353,10 @@ export class LibWrapperSettings extends FormApplication {
if(key in cfg_prioritized)
return;

// Handle legacy format, if found
if(!data.id) {
data = new PackageInfo(key, PACKAGE_TYPES.MODULE);
key = data.key;
// Check key/data validity
if(!is_valid_priority_key_data(key, data)) {
Log.warn$?.(`Ignoring '${key}' entry while loading module priorities due to invalid format.`)
return;
}

// Push data
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test_combinations('Library: Main', async function (t) {
// Unless the module has a higher priority
load_priorities({
prioritized: {
'm4': {index: 0}
'module:m4': {id: 'm4', title: 'm4', index: 0}
}
});
libWrapper.register('m4', 'A.prototype.x', chkr.gen_wr('m4:Ovr:4', {override: true}), 'OVERRIDE');
Expand Down

0 comments on commit f93c00c

Please sign in to comment.