1+ import { INITIAL_ACTOR_DATA } from '../constants.js' ; // ajusta el path si BaseType.js no está un nivel abajo
2+
3+ function stripSystemPrefix ( systemPath ) {
4+ return typeof systemPath === 'string' && systemPath . startsWith ( 'system.' )
5+ ? systemPath . slice ( 'system.' . length )
6+ : systemPath ;
7+ }
8+
9+ function parseTypeMarker ( str ) {
10+ try {
11+ const obj = JSON . parse ( str ) ;
12+ if ( ! obj || typeof obj !== 'object' ) return null ;
13+ return obj ; // { type, ...overrides }
14+ } catch {
15+ return null ;
16+ }
17+ }
18+
119export class BaseType {
220 static type = 'BaseType' ;
321
@@ -8,6 +26,54 @@ export class BaseType {
826
927 this . actor = actor ;
1028 this . systemPath = systemPath ;
29+
30+ // Cache to avoid parsing JSON every _get call
31+ this . _initialOverrideCache = null ;
32+ this . _initialOverrideCacheReady = false ;
33+ }
34+
35+ _resolveInitialOverrides ( ) {
36+ if ( this . _initialOverrideCacheReady ) return this . _initialOverrideCache ;
37+
38+ this . _initialOverrideCacheReady = true ;
39+ this . _initialOverrideCache = null ;
40+
41+ const relPath = stripSystemPrefix ( this . systemPath ) ;
42+ if ( ! relPath ) return this . _initialOverrideCache ;
43+
44+ const node = foundry . utils . getProperty ( INITIAL_ACTOR_DATA , relPath ) ;
45+ const markerStr = node ?. __type ;
46+ if ( typeof markerStr !== 'string' ) return this . _initialOverrideCache ;
47+
48+ const marker = parseTypeMarker ( markerStr ) ;
49+ if ( ! marker ) return this . _initialOverrideCache ;
50+
51+ // Remove "type", keep only overrides
52+ const { type, ...overrides } = marker ;
53+ this . _initialOverrideCache = overrides ;
54+
55+ return this . _initialOverrideCache ;
56+ }
57+
58+ _get ( relPath , fallback = undefined ) {
59+ const actorValue = foundry . utils . getProperty (
60+ this . actor ,
61+ `${ this . systemPath } .${ relPath } `
62+ ) ;
63+ if ( actorValue !== undefined ) return actorValue ;
64+
65+ // Fallback to INITIAL_ACTOR_DATA overrides (from __type marker), if any
66+ const overrides = this . _resolveInitialOverrides ( ) ;
67+ if ( overrides ) {
68+ const overrideValue = foundry . utils . getProperty ( overrides , relPath ) ;
69+ if ( overrideValue !== undefined ) return overrideValue ;
70+ }
71+
72+ return fallback ;
73+ }
74+
75+ _set ( relPath , value ) {
76+ foundry . utils . setProperty ( this . actor , `${ this . systemPath } .${ relPath } ` , value ) ;
1177 }
1278
1379 // ---- Instance extra deps (per derived spec id) ----
@@ -43,16 +109,6 @@ export class BaseType {
43109 } ) ;
44110 }
45111
46- _get ( relPath , fallback = undefined ) {
47- return (
48- foundry . utils . getProperty ( this . actor , `${ this . systemPath } .${ relPath } ` ) ?? fallback
49- ) ;
50- }
51-
52- _set ( relPath , value ) {
53- foundry . utils . setProperty ( this . actor , `${ this . systemPath } .${ relPath } ` , value ) ;
54- }
55-
56112 /**
57113 * Returns derived calculation specs for effectFlow.
58114 * Subclasses should override and may return multiple specs (e.g. "final", "mod").
0 commit comments