*/}}

la_data.c 191 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "la_5.h"
  19. extern LA MAIN;
  20. const char* LA_UDF_EXTENSION_STRINGS[]={
  21. "LA_UDF_EXTENSION_FLOAT32",
  22. "LA_UDF_EXTENSION_RAW",
  23. "LA_UDF_EXTENSION_QUICK_SEEK",
  24. "",
  25. };
  26. int la_IsThisProp(const laProp *p, const char *id){
  27. return (!strcmp(p->Identifier, id));
  28. }
  29. laProp *la_PropLookup(laListHandle *lst, const char *ID){
  30. laProp *p = 0, *ip;
  31. if (!lst) return 0;
  32. p = lstFindItem(ID, la_IsThisProp, lst);
  33. if (!p) if (strSame(ID, "identifier")){
  34. for (ip = lst->pFirst; ip; ip = ip->Item.pNext){
  35. if (ip->Tag & LA_AS_IDENTIFIER) return ip;
  36. }
  37. }
  38. return p;
  39. }
  40. laProp *la_PropLookupIdentifierItem(laListHandle *lst){
  41. laProp *p;
  42. if (!lst) return 0;
  43. for (p = lst->pFirst; p; p = p->Item.pNext){
  44. if (p->Tag & LA_AS_IDENTIFIER) return p;
  45. elif (la_IsThisProp(p, "identifier")) return p;
  46. }
  47. return 0;
  48. }
  49. int la_IsThisContainer(laPropContainer *p, char *id){
  50. return (!strcmp(p->Identifier, id));
  51. }
  52. laPropContainer *la_ContainerLookup(const char *ID){
  53. laPropContainer* pc= lstFindItem(ID, la_IsThisContainer, &MAIN.PropContainers);
  54. return pc;
  55. }
  56. void la_CopyPropStepRecursive(laPropPack* From, laPropPack* To){
  57. if(From->RawThis){ la_CopyPropStepRecursive(From->RawThis,To); }
  58. laPropStep *ps,*fps,*LastPs=To->LastPs;
  59. for (fps = From->Go; fps; fps = fps->pNext) {
  60. ps = memAcquireSimple(sizeof(laPropStep));
  61. ps->p = fps->p;
  62. ps->UseInstance = fps->UseInstance;
  63. ps->Type = fps->Type;
  64. if (LastPs) LastPs->pNext = ps;
  65. else To->Go = ps;
  66. LastPs = ps;
  67. }
  68. To->LastPs = LastPs;
  69. }
  70. void la_CopyPropPack(laPropPack* From, laPropPack* To) {
  71. laPropStep* ps,*fps,*LastPs=0;
  72. To->EndInstance = From->EndInstance;
  73. To->Go = To->LastPs = 0;
  74. la_FreePropStepCache(To->Go);
  75. la_CopyPropStepRecursive(From, To);
  76. if(!To->LastPs){
  77. ps = memAcquireSimple(sizeof(laPropStep));
  78. ps->p = From->LastPs->p;
  79. ps->UseInstance = From->LastPs->UseInstance;
  80. ps->Type = From->LastPs->Type;
  81. To->LastPs = ps;
  82. }
  83. }
  84. laListHandle* laGetUserList(void* HyperUserMem, laProp* Which, int* IsLocal){
  85. laListHandle* users;
  86. if(Which && Which->Container){
  87. laPropContainer* pc=Which->Container;
  88. if(pc->OtherAlloc || (!pc->Hyper)){
  89. *IsLocal=1;
  90. return &pc->LocalUsers;
  91. }
  92. }
  93. *IsLocal=0;
  94. return memGetUserList(HyperUserMem);
  95. }
  96. int laCountUserSpecific(laListHandle* users, laProp* which, int* r_count){
  97. int spec=0,count=0;
  98. for(laItemUserLinker* iul=users->pFirst;iul;iul=iul->Pointer.pNext){
  99. if(which == iul->Which){ spec++; } count++;
  100. }
  101. if(r_count){ *r_count=count; }
  102. return spec;
  103. }
  104. laItemUserLinker *laUseDataBlock(void *HyperUserMem, laProp *Which, unsigned int FrameDistinguish, void *User, laUserRemoveFunc Remover, int ForceRecalc){
  105. laItemUserLinker *iul; laItemUserLinkerLocal *iull; int local;
  106. if (!HyperUserMem) return 0;
  107. laListHandle* users = laGetUserList(HyperUserMem, Which, &local); if(!users) return 0;
  108. for (iul = users->pFirst; iul; iul = iul->Pointer.pNext){
  109. if(local && (iull=iul) && iull->Instance!=HyperUserMem) continue;
  110. if (iul->Which == Which && iul->Pointer.p == User){
  111. iul->FrameDistinguish = FrameDistinguish;
  112. if(ForceRecalc) iul->ForceRecalc = ForceRecalc;
  113. return iul;
  114. }
  115. }
  116. iul = lstAppendPointerSized(users, User, local?sizeof(laItemUserLinkerLocal):sizeof(laItemUserLinker));
  117. iul->Which = Which;
  118. iul->FrameDistinguish = FrameDistinguish;
  119. iul->Remove = Remover;
  120. iul->ForceRecalc = ForceRecalc;
  121. if(local){ iull=iul; iull->Instance=HyperUserMem; }
  122. return iul;
  123. }
  124. void laStopUsingDataBlock(void *HyperUserMem, laProp *prop, laPanel* p){
  125. laItemUserLinker *iul,*next_iul; laItemUserLinkerLocal *iull; int local;
  126. if (!HyperUserMem) return;
  127. laListHandle* users = laGetUserList(HyperUserMem, prop, &local); if(!users) return;
  128. for (iul = users->pFirst; iul; iul = next_iul){
  129. next_iul = iul->Pointer.pNext;
  130. if(local && (iull=iul) && iull->Instance!=HyperUserMem) continue;
  131. if (/*prop == iul->Which &&*/ iul->Pointer.p == p||iul->Additional==p){
  132. lstRemoveItem(users,iul);
  133. memFree(iul);
  134. }
  135. }
  136. }
  137. void laDataBlockNoLongerExists(void *HyperUserMem, laListHandle* UserList){
  138. laItemUserLinker *iul,*next_iul;
  139. for (iul = UserList->pFirst; iul; iul = next_iul){
  140. next_iul = iul->Pointer.pNext;
  141. iul->Remove(HyperUserMem, iul);
  142. lstRemoveItem(UserList,iul);
  143. memFree(iul);
  144. }
  145. }
  146. void la_PrintUserList(void* HyperUserMem){
  147. laItemUserLinker* iul, * next_iul; laItemUserLinkerLocal* iull; int local;
  148. if (!HyperUserMem) return;
  149. laListHandle* users = laGetUserList(HyperUserMem, 0, &local); if (!users) return;
  150. for (iul = users->pFirst; iul; iul = next_iul) {
  151. next_iul = iul->Pointer.pNext;
  152. printf("p %llx which %s add %llx\n", iul->Pointer.p, iul->Which ? iul->Which->Identifier : "-", iul->Additional);
  153. }
  154. }
  155. //laProp* la_GetGeneralPropFromPath(laProp* General, const char * Path) {
  156. // laProp* p;
  157. // laListHandle* lst = &General->Sub;
  158. // laStringSplitor* ss = strSplitPathByDot(Path);
  159. // laStringPart* sp = ss->parts.pFirst;
  160. //
  161. // while (sp) {
  162. // p = la_PropLookup(lst, sp->Content);
  163. // lst = &p->Sub;
  164. // sp = sp->Item.pNext;
  165. // }
  166. //
  167. // strDestroyStringSplitor(&ss);
  168. // return p;
  169. //}
  170. //laProp* la_GetPropFromPath(laPropPack* Base,const char * Path) {
  171. // laProp* p,*LastP=Base?Base->P:0;
  172. // laListHandle* lst;
  173. // laStringSplitor* ss = strSplitPathByDot(Path);
  174. // laStringPart* sp = ss->parts.pFirst;
  175. //
  176. // if(!Base || !Base->P)
  177. // lst = &MAIN.DataRoot.Root->Sub;
  178. // else lst = &Base->P->Sub;
  179. //
  180. //
  181. // while (sp) {
  182. // p = la_PropLookup(lst, sp->Content);
  183. // if (!p && LastP) {
  184. // p = la_PropLookup(&la_GetGeneralProp(LastP)->Sub, sp->Content);
  185. // }
  186. // LastP = p;
  187. // lst = &p->Sub;
  188. // sp = sp->Item.pNext;
  189. // }
  190. //
  191. // strDestroyStringSplitor(&ss);
  192. //
  193. // return p;
  194. //}
  195. void la_NewPropStep(laPropPack *Self, laProp *P, void *UseInstance, char Type){
  196. laPropStep *ps = memAcquireSimple(sizeof(laPropStep));
  197. if (Type == U'.' || Type == U'$'){
  198. ps->p = P;
  199. ps->UseInstance = UseInstance;
  200. ps->Type = Type;
  201. }elif (Type == U'@' || Type == U'=' || Type == U'#'){
  202. ps->p = CreateNewBuffer(char, strlen(P) + 1);
  203. strcpy(ps->p, P);
  204. ps->UseInstance = UseInstance;
  205. ps->Type = Type;
  206. }
  207. if (!Self->Go){
  208. Self->Go = ps;
  209. Self->LastPs = ps;
  210. }else{
  211. Self->LastPs->pNext = ps;
  212. Self->LastPs = ps;
  213. }
  214. }
  215. void la_FreePropStepCache(laPropStep *GoTarget){
  216. if (!GoTarget) return;
  217. if (GoTarget->pNext){
  218. la_FreePropStepCache(GoTarget->pNext);
  219. }
  220. if (GoTarget->Type == U'@' || GoTarget->Type == U'=' || GoTarget->Type == U'#') FreeMem(GoTarget->p);
  221. memFree(GoTarget);
  222. }
  223. void *la_FindMatchingInstance(void *From, laProp *Sub, laProp *p, laPropStep *Value){
  224. laPropPack Fake;
  225. laPropStep FakePs;
  226. laPropIterator pi = {0};
  227. void *Inst;
  228. char _buf[LA_RAW_CSTR_MAX_LEN]={0}; char*buf=_buf;
  229. int val;
  230. Fake.LastPs = &FakePs;
  231. FakePs.p = p;
  232. Inst = laGetInstance(Sub, From, &pi);
  233. if (p->PropertyType == LA_PROP_STRING){
  234. _buf[0] = 0;
  235. FakePs.UseInstance = Inst;
  236. laGetString(&Fake, _buf, &buf);
  237. while (!strSame(buf, Value->p)){
  238. Inst = laGetNextInstance(Sub, Inst, &pi);
  239. if (!Inst) return 0;
  240. _buf[0] = 0;
  241. FakePs.UseInstance = Inst;
  242. laGetString(&Fake, _buf, &buf);
  243. }
  244. return Inst;
  245. }elif (p->PropertyType == LA_PROP_INT){
  246. FakePs.UseInstance = Inst;
  247. val = laGetInt(&Fake); char buf[64]; int compareval=0;
  248. int status=sscanf(buf,"%d",&compareval);
  249. while (status && status!=EOF && val != compareval){
  250. Inst = laGetNextInstance(Sub, From, &pi);
  251. if (!Inst) return 0;
  252. FakePs.UseInstance = Inst;
  253. val = laGetInt(&Fake);
  254. }
  255. return Inst;
  256. }
  257. return 0;
  258. }
  259. int la_GetPropFromPath(laPropPack *Self, laPropPack *Base, const char *Path, void **SpecifiedInstance){
  260. laProp *p = 0;
  261. laListHandle *lst=0, *lstB = 0;
  262. laStringSplitor *ss = strSplitPath(Path,0);
  263. laStringPart *sp = ss ? ss->parts.pFirst : 0;
  264. int InstanceNum = 0;
  265. if ((!ss && !Base) || !MAIN.DataRoot.Root){ strDestroyStringSplitor(&ss); return 0; }
  266. if (!Base || !Base->LastPs){
  267. lst = &MAIN.DataRoot.Root->Props;
  268. Self->RawThis = 0;
  269. }else{
  270. Self->RawThis = Base;
  271. if (Base->LastPs->p->PropertyType==LA_PROP_SUB && ((laSubProp*)Base->LastPs->p)->GetType && Base->EndInstance){
  272. laPropContainer* pc=((laSubProp*)Base->LastPs->p)->GetType(Base->EndInstance);
  273. lst = &pc->Props;
  274. lstB = pc ? &pc->Props : 0;
  275. }
  276. if(!lst){
  277. if (!Base->LastPs->p->SubProp){
  278. laProp *p = Base->LastPs->p;
  279. p->SubProp = la_ContainerLookup(((laSubProp *)p)->TargetID);
  280. }
  281. if(Base->LastPs->p->SubProp) lst = &Base->LastPs->p->SubProp->Props;
  282. }
  283. lstB = Base->LastPs->p->SubExtra ? &Base->LastPs->p->SubExtra->Props : 0;
  284. if (!Path || !Path[0] || !ss){
  285. if (Base){
  286. Self->LastPs = Base->LastPs;
  287. //Self->ReusedPs = 1;
  288. //la_NewPropStep(Self, Base->LastPs->p,Base->LastPs->UseInstance,Base->LastPs->Type);
  289. }
  290. }
  291. }
  292. while (sp){
  293. if (sp->Type == U'.' || sp->Type == U'$'){
  294. void *inst = 0;
  295. p = la_PropLookup(lst, sp->Content);
  296. if (!p) p = la_PropLookup(lstB, sp->Content);
  297. if (!p){
  298. la_FreePropStepCache(Self->Go);
  299. Self->Go = Self->LastPs = 0;
  300. strDestroyStringSplitor(&ss);
  301. return 0; // |-----------------Here Should Include Dollar Instance
  302. } // V
  303. if (sp->Type == U'$'){
  304. inst = SpecifiedInstance[InstanceNum];
  305. InstanceNum++;
  306. }else
  307. inst = 0;
  308. /*if(Base == &MAIN.PropMatcherContextP->PropLinkPP)la_NewPropStep(Self, p, inst, '*');
  309. else */
  310. la_NewPropStep(Self, p, inst, sp->Type);
  311. if (p->PropertyType == LA_PROP_SUB && !p->SubProp){
  312. p->SubProp = la_ContainerLookup(((laSubProp *)p)->TargetID);
  313. }
  314. lst = p->SubProp ? &p->SubProp->Props : 0;
  315. lstB = p->SubExtra ? &p->SubExtra->Props : 0;
  316. }else{
  317. if (sp->Type == U'@' || sp->Type == U'=' || sp->Type == U'#'){ // || sp->Type== U'~'
  318. la_NewPropStep(Self, sp->Content, 0, sp->Type);
  319. }
  320. }
  321. sp = sp->Item.pNext;
  322. }
  323. strDestroyStringSplitor(&ss);
  324. return 1;
  325. }
  326. void la_StepPropPack(laPropPack *pp){
  327. laPropStep *ps = pp->Go;
  328. laPropPack *This = pp->RawThis;
  329. laPropIterator pi;
  330. laProp *Lookup;
  331. laPropStep *Value;
  332. void *UseInstance = 0;
  333. if (This){
  334. UseInstance = This->EndInstance;
  335. if (ps) ps->UseInstance = UseInstance;
  336. }else{
  337. if (!ps) return;
  338. UseInstance = MAIN.DataRoot.RootInstance;
  339. ps->UseInstance = UseInstance;
  340. }
  341. if (This && !ps){
  342. pp->EndInstance = UseInstance;
  343. pp->LastPs = This->LastPs;
  344. return;
  345. }
  346. while (ps){
  347. // Looks like we have these kind of access grammar:
  348. // sub.prop
  349. // sub@prop=str_or_int
  350. // sub#identifier
  351. // TODO: Add sub~index grammar
  352. if (ps->p->PropertyType == LA_PROP_SUB){
  353. if (ps->Type == U'@' || ps->Type == U'#'){
  354. }
  355. if (ps->Type == U'$') UseInstance = ps->UseInstance;
  356. elif (ps->pNext && ps->pNext->Type != U'.'){
  357. if (ps->pNext->Type == U'@'){
  358. Value = ps->pNext->pNext;
  359. Lookup = la_PropLookup(&ps->p->SubProp->Props, ps->pNext->p);
  360. }elif (ps->pNext->Type == U'#'){
  361. Value = ps->pNext;
  362. Lookup = la_PropLookupIdentifierItem(&ps->p->SubProp->Props);
  363. }
  364. if (Value->Type == U'=' || Value->Type == U'#'){ //MUST!
  365. UseInstance = la_FindMatchingInstance(ps->UseInstance, ps->p, Lookup, Value);
  366. }
  367. Value->UseInstance = UseInstance;
  368. if(Value->pNext) Value->pNext->UseInstance = UseInstance;
  369. ps = Value->pNext;
  370. continue;
  371. }else{
  372. void *inst;
  373. if(((laSubProp*)ps->p)->IsDetached){ UseInstance = ((laSubProp*)ps->p)->Detached; }
  374. else{
  375. inst = laGetActiveInstanceStrict(ps->p, UseInstance);
  376. if (!inst) inst = laGetInstance(ps->p, UseInstance, 0);
  377. UseInstance = inst;
  378. }
  379. }
  380. if (!ps->pNext){
  381. pp->EndInstance = UseInstance;
  382. break;
  383. }else{
  384. ps->pNext->UseInstance = UseInstance;
  385. ps = ps->pNext;
  386. }
  387. continue;
  388. }else{
  389. ps->UseInstance = UseInstance;
  390. if (!ps->pNext){
  391. pp->EndInstance = UseInstance;
  392. break;
  393. }else{
  394. ps->pNext->UseInstance = ps;
  395. UseInstance = ps;
  396. ps = ps->pNext;
  397. }
  398. }
  399. }
  400. }
  401. void la_RemovePropUserNode(laPropStep *ps){
  402. laPropUserPanel *pup = 0;
  403. laPropUser *pu = 0;
  404. for (pup = ps->p->UserPanels.pFirst; pup; pup = pup->Item.pNext){
  405. if (pup->Panel == MAIN.PropMatcherContextP) break;
  406. }
  407. if (!pup) return;
  408. for (pu = pup->UserPacks.pFirst; pu; pu = pu->Item.pNext){
  409. if (pu->User == ps) break;
  410. }
  411. //if (!pu)printf("PROP USER NODE NOT FOUND!\laPropUserPanel:%d,%s,Ps:%s\n",pup->Panel,pup->Panel->Title->Ptr,ps->p->Identifier);
  412. //else {
  413. if (pu){
  414. lstRemoveItem(&pup->UserPacks, pu);
  415. memFree(pu);
  416. if (!pup->UserPacks.pFirst){
  417. lstRemoveItem(&ps->p->UserPanels, pup);
  418. memFree(pup);
  419. }
  420. }
  421. //}
  422. }
  423. void la_AddPropUserNode(laPropStep *ps){
  424. laPropUserPanel *pup = 0;
  425. laPropUser *pu = 0;
  426. for (pup = ps->p->UserPanels.pFirst; pup; pup = pup->Item.pNext){
  427. if (pup->Panel == MAIN.PropMatcherContextP) break;
  428. }
  429. if (!pup){
  430. pup = memAcquireSimple(sizeof(laPropUserPanel));
  431. pup->Panel = MAIN.PropMatcherContextP;
  432. lstAppendItem(&ps->p->UserPanels, pup);
  433. }
  434. for (pu = pup->UserPacks.pFirst; pu; pu = pu->Item.pNext){
  435. if (pu->User == ps){
  436. pu->FrameDistinguish = MAIN.PropMatcherContextP->FrameDistinguish;
  437. return;
  438. }
  439. }
  440. //ELSE
  441. pu = memAcquireSimple(sizeof(laPropUser));
  442. pu->User = ps;
  443. pu->FrameDistinguish = MAIN.PropMatcherContextP->FrameDistinguish;
  444. lstAppendItem(&pup->UserPacks, pu);
  445. }
  446. void la_UsePropPack(laPropPack *pp, int ForceRecalc){
  447. laPropStep *ps = 0;
  448. //if(pp->LastPs && pp->LastPs->p)printf("%s\n",pp->LastPs->p->Identifier);
  449. if((!pp->Go) && pp->LastPs){
  450. if (pp->LastPs->Type==U'.'){
  451. laUseDataBlock(pp->RawThis->LastPs->UseInstance, pp->LastPs->p, MAIN.PropMatcherContextP->FrameDistinguish, MAIN.PropMatcherContextP, la_PropPanelUserRemover, ForceRecalc);
  452. }
  453. }
  454. for (ps = pp->Go; ps; ps = ps->pNext){
  455. if (ps->Type==U'.'){
  456. laUseDataBlock(ps->UseInstance, ps->p, MAIN.PropMatcherContextP->FrameDistinguish, MAIN.PropMatcherContextP, la_PropPanelUserRemover, ForceRecalc);
  457. }
  458. }
  459. }
  460. void la_StopUsingPropPack(laPropPack *pp){
  461. laPropStep *ps;
  462. if((!pp->Go) && pp->LastPs){
  463. if (pp->LastPs->Type==U'.'&&pp->LastPs->p->Container &&pp->LastPs->p->Container->Hyper){
  464. laStopUsingDataBlock(pp->RawThis->LastPs->UseInstance, pp->LastPs->p, MAIN.PropMatcherContextP);
  465. }
  466. }
  467. for (ps = pp->Go; ps; ps = ps->pNext){
  468. if (ps->Type==U'.'&&ps->p->Container&&ps->p->Container->Hyper){
  469. laStopUsingDataBlock(ps->UseInstance, ps->p, MAIN.PropMatcherContextP);
  470. }
  471. //la_RemovePropUserNode(ps);
  472. }
  473. }
  474. void laNotifyUsersPP(laPropPack *pp){
  475. if(!pp->LastPs) return;
  476. void *hi = pp->LastPs->UseInstance;
  477. laItemUserLinker *iul; laItemUserLinkerLocal *iull; int local;
  478. if (!hi) return;
  479. laListHandle* users = laGetUserList(hi, pp->LastPs->p, &local); if(!users) return;
  480. for (iul = users->pFirst; iul; iul = iul->Pointer.pNext){
  481. if(local && (iull=iul) && iull->Instance!=hi) continue;
  482. if (iul->Remove == la_PropPanelUserRemover){
  483. laPanel *p = iul->Pointer.p;
  484. if (iul->Which == pp->LastPs->p && iul->FrameDistinguish == p->FrameDistinguish){
  485. //laSpinLock(&MAIN.csNotifier);
  486. if ((iul->Which->PropertyType & LA_PROP_SUB) || iul->ForceRecalc) laRecalcPanel(p);
  487. else laRedrawPanel(p);
  488. //laSpinUnlock(&MAIN.csNotifier);
  489. }
  490. }
  491. }
  492. }
  493. void laNotifyUsersPPPath(laPropPack *pp, char *path){
  494. laPropPack PP = {0};
  495. if(!path){ laNotifyUsersPP(pp); return; }
  496. la_GetPropFromPath(&PP, pp, path, 0);
  497. la_StepPropPack(&PP);
  498. laNotifyUsersPP(&PP);
  499. la_FreePropStepCache(PP.Go);
  500. }
  501. void laNotifySubPropUsers(laProp *prop, void *Instance){
  502. void *hi = Instance;
  503. laItemUserLinker *iul; laItemUserLinkerLocal *iull; int local;
  504. if (!hi) return;
  505. laListHandle* users = laGetUserList(hi, prop, &local); if(!users) return;
  506. for (iul = users->pFirst; iul; iul = iul->Pointer.pNext){
  507. if(local && (iull=iul) && iull->Instance!=hi) continue;
  508. if (iul->Remove == la_PropPanelUserRemover){
  509. laPanel *p = iul->Pointer.p;
  510. if (iul->Which == prop && iul->FrameDistinguish == p->FrameDistinguish){
  511. //laSpinLock(&MAIN.csNotifier);
  512. laRecalcPanel(p);
  513. //laSpinUnlock(&MAIN.csNotifier);
  514. }
  515. }
  516. }
  517. }
  518. void laNotifyInstanceUsers(void *Instance){
  519. void *hi = Instance;
  520. laItemUserLinker *iul; laItemUserLinkerLocal *iull; int local; if (!hi) return;
  521. laListHandle* users = laGetUserList(hi, 0, &local); if(!users) return;
  522. for (iul = users->pFirst; iul; iul = iul->Pointer.pNext){
  523. if(local && (iull=iul) && iull->Instance!=hi) continue;
  524. if (iul->Remove == la_PropPanelUserRemover){
  525. laPanel *p = iul->Pointer.p;
  526. if (iul->FrameDistinguish == p->FrameDistinguish){
  527. laRedrawPanel(p);
  528. if(iul->Which&&iul->Which->PropertyType==LA_PROP_SUB){ laRecalcPanel(p); }
  529. }
  530. }
  531. }
  532. }
  533. void laNotifyUsers(char *Path){
  534. laPropPack PP = {0};
  535. la_GetPropFromPath(&PP, 0, Path, 0);
  536. la_StepPropPack(&PP);
  537. laNotifyUsersPP(&PP);
  538. la_FreePropStepCache(PP.Go);
  539. }
  540. void laThreadNotifyUsers(char *Path){
  541. laThreadNotifier *tn = CreateNew(laThreadNotifier);
  542. strCopyFull(tn->Path, Path);
  543. //laSpinLock(&MAIN.csNotifier);
  544. lstAppendItem(&MAIN.ThreadNotifiers, tn);
  545. //laSpinUnlock(&MAIN.csNotifier);
  546. }
  547. void la_PanelResetDetachedReference(laPanel*p,void*Old,void*New){
  548. for(laSubProp* sp=p->PropLinkContainer->Props.pFirst;sp;sp=sp->Base.Item.pNext){
  549. if(sp->Base.PropertyType!=LA_PROP_SUB) continue;
  550. if(sp->Detached == Old){ memAssignRef(sp, &sp->Detached, New); }
  551. }
  552. for(laPanel* sp=p->SubPanels.pFirst;sp;sp=sp->Item.pNext){
  553. la_PanelResetDetachedReference(sp,Old,New);
  554. }
  555. }
  556. void la_NotifyDetachedRecursive(laBlock* b, void* Old, void* New){
  557. if(b->B1){ la_NotifyDetachedRecursive(b->B1,Old,New);la_NotifyDetachedRecursive(b->B2,Old,New); return; }
  558. for(laPanel* p=b->Panels.pFirst;p;p=p->Item.pNext){
  559. la_PanelResetDetachedReference(p,Old,New);
  560. }
  561. }
  562. void laNotifyDetached(void* OldInstance, void* NewInstance){
  563. for(laWindow* w=MAIN.Windows.pFirst;w;w=w->Item.pNext){
  564. for(laLayout* l=w->Layouts.pFirst;l;l=l->Item.pNext){
  565. la_NotifyDetachedRecursive(l->FirstBlock,OldInstance,NewInstance);
  566. }
  567. for(laPanel* p=w->Panels.pFirst;p;p=p->Item.pNext){
  568. la_PanelResetDetachedReference(p,OldInstance,NewInstance);
  569. }
  570. }
  571. }
  572. void laDetachedTrySet(char* prop_identifier,void* NewInstance){
  573. laPanel* p=MAIN.CurrentPanel; for(laSubProp* sp=p->PropLinkContainer->Props.pFirst;sp;sp=sp->Base.Item.pNext){
  574. if(sp->Base.PropertyType!=LA_PROP_SUB){ continue; }
  575. if(strSame(sp->Base.Identifier,prop_identifier)){ memAssignRef(sp,&sp->Detached,NewInstance); }
  576. }
  577. }
  578. void la_SetPropMathcerContext(laPanel *p){
  579. MAIN.PropMatcherContextP = p;
  580. }
  581. int laget_InstanceHyperLevel(void* instance){
  582. int level; memGetHead(instance,&level);
  583. return level;
  584. }
  585. int laget_InstanceModified(void* instance){
  586. int level; laMemNodeHyper* m=memGetHead(instance,&level);
  587. if(level==2) return m->Modified?1:0;
  588. return 0;
  589. }
  590. void* laget_InstanceUDF(void* unused, laPropIterator* unusedpi){
  591. return MAIN.ManagedUDFs.pFirst;
  592. }
  593. void* laget_InstanceActiveUDF(void* instance){
  594. int level; laMemNodeHyper* m=memGetHead(instance,&level);
  595. if(level==2) return m->FromFile;
  596. return 0;
  597. }
  598. void* laget_DummyManagedUDFSingle(void* unused){
  599. return MAIN.DummyManageUDFSingle;
  600. }
  601. void* laget_DummyManagedUDFSingleForce(void* unused){
  602. return MAIN.DummyManageUDFSingleForce;
  603. }
  604. void laset_InstanceUDFFromSingle(void* instance, laUDF* udf){
  605. if(!MAIN._CONTAINER_SETTING || !MAIN._CONTAINER_SETTING->UDFPropagate || !instance){ return; }
  606. if(udf == MAIN.DummyManageUDF){
  607. laInvoke(0, "LA_managed_save_new_file", 0,0,0,0);
  608. }
  609. MAIN._CONTAINER_SETTING->UDFPropagate(instance,udf,0);
  610. }
  611. void laset_InstanceUDFFromSingleForce(void* instance, laUDF* udf){
  612. if(!MAIN._CONTAINER_SETTING || !MAIN._CONTAINER_SETTING->UDFPropagate || !instance){ return; }
  613. if(udf == MAIN.DummyManageUDF){
  614. laInvoke(0, "LA_managed_save_new_file", 0,0,0,0);
  615. }
  616. MAIN._CONTAINER_SETTING->UDFPropagate(instance,udf,0);
  617. }
  618. void laset_InstanceUDF(void* instance, void* set){
  619. int level; laMemNodeHyper* m=memGetHead(instance,&level);
  620. if(level==2) memAssignRef(instance, &m->FromFile, set);
  621. if(set == MAIN.DummyManageUDF){
  622. laInvoke(0, "LA_managed_save_new_file", 0,0,0,0);
  623. }
  624. }
  625. void* laget_InstanceSelf(void* instace){
  626. return instace;
  627. }
  628. const char* LA_N_A="N/A";
  629. void *la_GetReadDBInstNUID(char *ReferReadNUID);
  630. void laget_InstanceUID(void* instance, char* buf, const char** ptr){
  631. int level; laMemNodeHyper* m=memGetHead(instance,&level);
  632. if(level!=2||!m) return; *ptr=LA_N_A;
  633. *ptr=m->NUID.String;
  634. }
  635. void laset_InstanceUID(void* instance, char* buf){
  636. int level; laMemNodeHyper* m=memGetHead(instance,&level);
  637. if(level!=2||!m) return;
  638. char _buf[40];
  639. if(!buf[0]){ memCreateNUID(_buf,m); buf=_buf; }
  640. laListHandle *l = hsh65536DoHashNUID(&MAIN.DBInst2, m->NUID.String);
  641. laMemNodeHyper* im=0;
  642. for (im = l->pFirst; im; im = m->Item.pNext){ if(im==m) break; }
  643. if(im){ lstRemoveItem(l,im); laListHandle *nl = hsh65536DoHashNUID(&MAIN.DBInst2, buf); lstAppendItem(nl,im); }else{ im=m; /* Unlikely */ }
  644. sprintf(m->NUID.String,"%.30s",buf);
  645. }
  646. void* laget_SaverDummy(void* instance, laPropIterator* pi){
  647. laSubProp* p=MAIN._PROP_SETTING; laListHandle* l = (((char*)instance)+p->ListHandleOffset);
  648. if(!l->pFirst){
  649. laSaverDummy* sd=memAcquireHyper(sizeof(laSaverDummy)); lstAppendItem(l,sd);
  650. }
  651. while(l->pFirst!=l->pLast){ memLeave(lstPopItem(l)); }
  652. return l->pFirst;
  653. }
  654. laSaverDummy* laGetSaverDummy(void* instance, laSubProp* p){
  655. laListHandle* l = (((char*)instance)+p->ListHandleOffset);
  656. if(!l->pFirst){ laSaverDummy* sd=memAcquireHyper(sizeof(laSaverDummy)); lstAppendItem(l,sd); }
  657. while(l->pFirst!=l->pLast){ memLeave(lstPopItem(l)); }
  658. return l->pFirst;
  659. }
  660. void laPurgeSaverDummy(void* instance, laSubProp* p){
  661. laListHandle* l = (((char*)instance)+p->ListHandleOffset);
  662. while(l->pFirst){ memLeave(lstPopItem(l)); }
  663. }
  664. void la_FreeProperty(laProp* p){
  665. laIntProp *ip;laFloatProp *fp;laEnumProp *ep;laSubProp *sp;
  666. if(p->DetachedPP.Go){
  667. la_FreePropStepCache(p->DetachedPP.Go);
  668. switch (p->PropertyType){
  669. case LA_PROP_INT:
  670. case LA_PROP_INT | LA_PROP_ARRAY:
  671. ip = p; free(ip->Detached);
  672. break;
  673. case LA_PROP_FLOAT:
  674. case LA_PROP_FLOAT | LA_PROP_ARRAY:
  675. fp = p; free(fp->Detached);
  676. break;
  677. case LA_PROP_ENUM:
  678. case LA_PROP_ENUM | LA_PROP_ARRAY:
  679. ep = p; free(ep->Detached);
  680. }
  681. }
  682. if(p->PropertyType&LA_PROP_ENUM){ ep = p; laEnumItem* ei; while(ei=lstPopItem(&ep->Items)){ memFree(ei); } }
  683. memFree(p);
  684. }
  685. void la_FreePropertyContainer(laPropContainer* pc){
  686. laProp* p;
  687. while(p=lstPopItem(&pc->Props)){
  688. la_FreeProperty(p);
  689. }
  690. memFree(pc);
  691. }
  692. laPropContainer *laAddPropertyContainer(const char *Identifier, const char *Name, const char *Description, uint32_t IconID,
  693. laUiDefineFunc DefaultUiDefine,
  694. int NodeSize, laContainerPostReadFunc PostRead, laContainerPostReadFunc PostReadIm, int IsHyper){
  695. laPropContainer *pc = memAcquire(sizeof(laPropContainer));
  696. pc->Identifier = Identifier;
  697. pc->Name = Name;
  698. pc->Description = Description;
  699. pc->UiDefine = DefaultUiDefine;
  700. pc->IconID = IconID ? IconID : U'📦';
  701. pc->OtherAlloc = (IsHyper&LA_PROP_OTHER_ALLOC)?1:0;
  702. pc->Hyper = pc->OtherAlloc?0:(IsHyper&LA_PROP_HYPER_BITS);
  703. pc->PostRead = PostRead;
  704. pc->PostReadIm = PostReadIm;
  705. pc->NodeSize = NodeSize;
  706. if (!pc->OtherAlloc){
  707. laAddSubGroup(pc, "__file", "File", "The file this block came from/saves to", "managed_udf",
  708. 0,LA_WIDGET_COLLECTION_SELECTOR,laui_ManagedUDFItem,-1,laget_InstanceUDF,laget_InstanceActiveUDF,laget_ListNext,laset_InstanceUDF,0,0,0,LA_UDF_REFER|LA_UDF_IGNORE);
  709. laAddIntProperty(pc, "__hyper", "Hyper", "Hyper level of this data block", 0,0,0,0,0,0,0,0,-1,laget_InstanceHyperLevel,0,0,0,0,0,0,0,0,0,LA_READ_ONLY|LA_UDF_IGNORE);
  710. laProp* p=laAddEnumProperty(pc, "__modified", "Modified", "Data block is modified", LA_WIDGET_ENUM_ICON_PLAIN,0,0,0,0,-1,laget_InstanceModified,0,0,0,0,0,0,0,0,LA_READ_ONLY|LA_UDF_IGNORE);
  711. laAddEnumItemAs(p, "MODIFIED", "Modified", "Data block is modified", 1, U'🌑');
  712. laAddEnumItemAs(p, "CLEAN", "Clean", "Data block is clean", 0, 0);
  713. laAddStringProperty(pc, "__uid","UID","UID for shared resoure lookup",0,0,0,0,0,-1,0,laget_InstanceUID,laset_InstanceUID,0,LA_UDF_IGNORE);
  714. }
  715. laAddSubGroup(pc, "__self", "Self", "Own instance", Identifier,0,0,0,-1,0,laget_InstanceSelf,0,0,0,0,0,LA_UDF_REFER|LA_UDF_IGNORE);
  716. lstAppendItem(&MAIN.PropContainers, pc);
  717. return pc;
  718. }
  719. void laPropContainerExtraFunctions(laPropContainer* pc, laContainerBeforeFreeF BeforeFree, laContainerResetF Reset, laContainerUndoTouchedF Touched, laContainerpUDFPropagateF UDFPropagate, laUiDefineFunc MenuUi){
  720. pc->BeforeFree=BeforeFree; pc->Reset=Reset; pc->UndoTouched=Touched; pc->UDFPropagate=UDFPropagate; pc->MenuUiDefine=MenuUi;
  721. if(!pc->OtherAlloc && UDFPropagate){
  722. laAddOperatorProperty(pc,"__udf_propagate","Propagate","Propagate UDF to all child nodes", "LA_udf_propagate", 0,0);
  723. laAddSubGroup(pc,"__single_udf_propagate","Save to","Assign file to all child nodes (From a ingle-instanced parent)","managed_udf",
  724. 0,LA_WIDGET_COLLECTION_SELECTOR,laui_ManagedUDFItem,-1,laget_InstanceUDF,laget_DummyManagedUDFSingle,laget_ListNext,laset_InstanceUDFFromSingle,0,0,0,LA_UDF_REFER|LA_UDF_IGNORE);
  725. //laAddSubGroup(pc,"__single_udf_propagate_force","Force","Assign file to all child nodes (From a ingle-instanced parent)","managed_udf",
  726. // 0,LA_WIDGET_COLLECTION_SELECTOR,laui_ManagedUDFItem,-1,laget_InstanceUDF,laget_DummyManagedUDFSingleForce,laget_ListNext,laset_InstanceUDFFromSingleForce,0,0,0,LA_UDF_REFER|LA_UDF_IGNORE);
  727. }
  728. }
  729. laProp* laPropContainerManageable(laPropContainer* pc, int offset_of_dummy_list){
  730. if(!pc || pc->OtherAlloc || !offset_of_dummy_list) return 0;
  731. if(!MAIN.SaverDummyContainer){
  732. MAIN.SaverDummyContainer = laAddPropertyContainer("la_saver_dummy","Saver Dummy","Saver dummy",0,0,sizeof(laSaverDummy),0,0,2);
  733. laAddSubGroup(MAIN.SaverDummyContainer, "__file", "File", "The file this block came from/saves to", "managed_udf",
  734. 0,LA_WIDGET_COLLECTION_SELECTOR,laui_ManagedUDFItem,-1,laget_InstanceUDF,laget_InstanceActiveUDF,laget_ListNext,laset_InstanceUDF,0,0,0,LA_UDF_REFER|LA_UDF_IGNORE);
  735. }
  736. pc->SaverDummy=laAddSubGroup(pc,"__single_saver_dummy","Saver","Saver dummy","la_saver_dummy",0,0,0,-1,0,0,0,0,0,0,offset_of_dummy_list,0);
  737. return pc->SaverDummy;
  738. }
  739. void laContainerAnimationFunctions(laPropContainer* pc, laActionHolderVerifyF ActionHolderVerify){
  740. pc->ActionHolderVerify=ActionHolderVerify;
  741. }
  742. int la_GetKeyablePropertyStorageSize(laProp* p){
  743. switch(p->PropertyType){
  744. case LA_PROP_INT: case LA_PROP_FLOAT: case LA_PROP_ENUM:
  745. return sizeof(uint64_t);
  746. case LA_PROP_INT | LA_PROP_ARRAY: case LA_PROP_FLOAT | LA_PROP_ARRAY: case LA_PROP_ENUM | LA_PROP_ARRAY:
  747. if(p->GetLen){ return 0; }
  748. return sizeof(uint64_t)*p->Len;
  749. case LA_PROP_SUB: case LA_PROP_OPERATOR: case LA_PROP_STRING: case LA_PROP_RAW: default:
  750. return 0;
  751. }
  752. }
  753. int la_GetPropertySize(int Type){
  754. switch (Type){
  755. case LA_PROP_INT:
  756. case LA_PROP_INT | LA_PROP_ARRAY:
  757. return sizeof(laIntProp);
  758. case LA_PROP_FLOAT:
  759. case LA_PROP_FLOAT | LA_PROP_ARRAY:
  760. return sizeof(laFloatProp);
  761. case LA_PROP_ENUM:
  762. case LA_PROP_ENUM | LA_PROP_ARRAY:
  763. return sizeof(laEnumProp);
  764. case LA_PROP_SUB: return sizeof(laSubProp);
  765. case LA_PROP_OPERATOR: return sizeof(laOperatorProp);
  766. case LA_PROP_STRING: return sizeof(laStringProp);
  767. case LA_PROP_RAW: return sizeof(laRawProp);
  768. }
  769. return 0;
  770. }
  771. const char LA_PROP_STR_INT[] = "LA_PROP_INT";
  772. const char LA_PROP_STR_INT_ARR[] = "LA_PROP_INT_ARR";
  773. const char LA_PROP_STR_FLOAT[] = "LA_PROP_FLOAT";
  774. const char LA_PROP_STR_FLOAT_ARR[] = "LA_PROP_FLOAT_ARR";
  775. const char LA_PROP_STR_ENUM[] = "LA_PROP_ENUM";
  776. const char LA_PROP_STR_ENUM_ARR[] = "LA_PROP_ENUM_ARR";
  777. const char LA_PROP_STR_ACT[] = "LA_PROP_ACT";
  778. const char LA_PROP_STR_STR[] = "LA_PROP_STR";
  779. const char LA_PROP_STR_SUB[] = "LA_PROP_SUB";
  780. const char *la_GetPropertyTypeString(int Type){
  781. switch (Type){
  782. case LA_PROP_INT:
  783. return LA_PROP_STR_INT;
  784. case LA_PROP_INT | LA_PROP_ARRAY:
  785. return LA_PROP_STR_INT_ARR;
  786. case LA_PROP_FLOAT:
  787. return LA_PROP_STR_FLOAT;
  788. case LA_PROP_FLOAT | LA_PROP_ARRAY:
  789. return LA_PROP_STR_FLOAT_ARR;
  790. case LA_PROP_ENUM:
  791. return LA_PROP_STR_ENUM;
  792. case LA_PROP_ENUM | LA_PROP_ARRAY:
  793. return LA_PROP_STR_ENUM_ARR;
  794. case LA_PROP_SUB:
  795. return LA_PROP_STR_SUB;
  796. case LA_PROP_OPERATOR:
  797. return LA_PROP_STR_ACT;
  798. case LA_PROP_STRING:
  799. return LA_PROP_STR_STR;
  800. }
  801. return 0;
  802. }
  803. void la_AssignPropertyGeneralSub(laProp *p){
  804. switch (p->PropertyType){
  805. case LA_PROP_INT: p->SubProp = MAIN.GeneralIntSub; break;
  806. case LA_PROP_INT | LA_PROP_ARRAY: p->SubProp = MAIN.GeneralIntArraySub; break;
  807. case LA_PROP_FLOAT: p->SubProp = MAIN.GeneralFloatSub; break;
  808. case LA_PROP_FLOAT | LA_PROP_ARRAY: p->SubProp = MAIN.GeneralFloatArraySub; break;
  809. case LA_PROP_ENUM: p->SubProp = MAIN.GeneralEnumSub; break;
  810. case LA_PROP_ENUM | LA_PROP_ARRAY: p->SubProp = MAIN.GeneralEnumArraySub; break;
  811. case LA_PROP_STRING: p->SubProp = MAIN.GeneralStringSub; break;
  812. case LA_PROP_OPERATOR: p->SubProp = MAIN.GeneralOperatorSub; break;
  813. case LA_PROP_SUB: p->SubExtra = MAIN.GeneralCollectionSub; break;
  814. case LA_PROP_RAW: p->SubExtra = MAIN.GeneralRawSub; break;
  815. }
  816. }
  817. laProp *la_CreateProperty(laPropContainer *Container, int Type, const char *Identifier, const char *Name, const char *Description,
  818. const char *Prefix, const char *Unit, laWidget* DefaultWidget, u64bit Tag){
  819. laProp *p = memAcquire(la_GetPropertySize(Type));
  820. p->Identifier = Identifier;
  821. p->Name = Name;
  822. p->Description = Description;
  823. p->Prefix = Prefix;
  824. p->Unit = Unit;
  825. if(DefaultWidget){ p->DefaultUiType = DefaultWidget->Type; p->DefaultFlags = DefaultWidget->Flags; }
  826. p->PropertyType = Type;
  827. p->Tag = Tag;
  828. p->Container = Container;
  829. if (Container) lstAppendItem(&Container->Props, p);
  830. p->UDFIsRefer = (Tag & LA_UDF_REFER) ? 1 : 0;
  831. p->UDFIgnore = (Tag & LA_UDF_IGNORE) ? 1 : 0;
  832. p->UDFOnly = (Tag & LA_UDF_ONLY) ? 1 : 0;
  833. p->ReadOnly = (Tag & LA_READ_ONLY) ? 1 : 0;
  834. p->IsRadAngle = (Tag & LA_RAD_ANGLE)? 1 : 0;
  835. p->UDFHideInSave = ((Tag & LA_HIDE_IN_SAVE) || p->UDFIgnore)? 1 : 0;
  836. p->UDFReadProgress = (Tag & LA_PROP_READ_PROGRESS)?1:0;
  837. p->CanTranslate = (Tag & LA_TRANSLATE)?1:0;
  838. if(p->IsRadAngle&&(!p->Unit)) p->Unit="°";
  839. p->Keyable = (Tag & LA_PROP_KEYABLE)? 1 : 0;
  840. if(Tag&LA_PROP_ROTATION){ p->KeySpecialInterpolation=LA_PROP_KEY_INTERPOLATION_ROTATION; }
  841. return p;
  842. }
  843. void la_ClearDetachedProp(laPanel* p){
  844. laProp *np; while(np=lstPopItem(&p->PropLinkContainer->Props)){ free(np->Identifier); memFree(np); }
  845. }
  846. laProp *la_MakeDetachedProp(laPanel* p, const char *From, const char *Rename){
  847. laIntProp *ip;
  848. laFloatProp *fp;
  849. laEnumProp *ep;
  850. laSubProp *sp;
  851. laPropPack TempPP = {0};
  852. int result = 0;
  853. result = la_GetPropFromPath(&TempPP, 0, From, 0);
  854. if(!result) return 0;
  855. laProp *np = memAcquire(la_GetPropertySize(TempPP.LastPs->p->PropertyType));
  856. memcpy(np, TempPP.LastPs->p, la_GetPropertySize(TempPP.LastPs->p->PropertyType));
  857. np->Identifier = CreateNewBuffer(char, 128);
  858. strcpy(np->Identifier, Rename);
  859. np->DetachedPP.Go = 0;
  860. np->DetachedPP.RawThis = 0;
  861. np->DetachedPP.LastPs = 0;
  862. la_CopyPropPack(&TempPP, &np->DetachedPP);
  863. switch (np->PropertyType){
  864. case LA_PROP_INT:
  865. case LA_PROP_INT | LA_PROP_ARRAY:
  866. ip = np; if(!np->Len){ np->Len=1; }
  867. ip->Detached = CreateNewBuffer(int, np->Len);
  868. for(int i=0;i<np->Len;i++){ ((int*)ip->Detached)[i]=ip->DefVal; }
  869. break;
  870. case LA_PROP_FLOAT:
  871. case LA_PROP_FLOAT | LA_PROP_ARRAY:
  872. fp = np; if(!np->Len){ np->Len=1; }
  873. fp->Detached = CreateNewBuffer(real, np->Len);
  874. for(int i=0;i<np->Len;i++){ ((real*)fp->Detached)[i]=fp->DefVal; }
  875. break;
  876. case LA_PROP_ENUM:
  877. case LA_PROP_ENUM | LA_PROP_ARRAY:
  878. ep = np;
  879. ep->Detached = CreateNewBuffer(int, 1);
  880. ep->Detached[0] = ep->DefVal;
  881. break;
  882. case LA_PROP_SUB:
  883. sp=np; sp->GetCategory=((laSubProp*)np)->GetCategory;
  884. sp->IsDetached=1;
  885. break;
  886. }
  887. lstAppendItem(&p->PropLinkContainer->Props, np);
  888. la_AssignPropertyGeneralSub(np);
  889. la_FreePropStepCache(TempPP.Go);
  890. return np;
  891. }
  892. laProp *laAddIntProperty(laPropContainer *Container, const char *Identifier, const char *Name, const char *Description, laWidget* DefaultWidget,
  893. const char *Prefix, const char *Unit, int Max, int Min, int Step, int DefVal, const int *DefArr,
  894. int OffsetSize, laIntGetF Get, laIntSetF Set, int ArrayLength, laArrayGetLenF GetLen,
  895. laIntArraySetF SetArr, laIntArrayGetAllF GetAll, laIntArraySetAllF SetAll, laIntArraySetAllArrayF SetAllArr,
  896. laIntReadF Read, laIntArrayReadAllF ReadAll,
  897. u64bit Tag){
  898. laIntProp *p = la_CreateProperty(Container, LA_PROP_INT, Identifier, Name, Description, Prefix, Unit, DefaultWidget, Tag);
  899. if (ArrayLength > 1){
  900. p->Base.Len = ArrayLength;
  901. p->Base.PropertyType |= LA_PROP_ARRAY;
  902. }else if (GetLen)
  903. p->Base.GetLen = GetLen;
  904. p->Get = Get;
  905. p->Set = Set;
  906. p->SetAll = SetAll;
  907. p->GetAll = GetAll;
  908. p->SetAllArr = SetAllArr;
  909. p->SetArr = SetArr;
  910. p->DefArr = DefArr;
  911. p->DefVal = DefVal;
  912. p->Max = Max;
  913. p->Min = Min;
  914. p->Step = Step ? Step : 1;
  915. p->Base.Offset = OffsetSize;
  916. p->Base.OffsetIsPointer = (Tag&LA_UDF_REFER) ? 1 : 0;
  917. p->Read = Read;
  918. p->ReadAll = ReadAll;
  919. la_AssignPropertyGeneralSub(p);
  920. return p;
  921. }
  922. laProp *laAddFloatProperty(laPropContainer *Container, const char *Identifier, const char *Name, const char *Description, laWidget* DefaultWidget,
  923. const char *Prefix, const char *Unit, real Max, real Min, real Step, real DefVal, const real *DefArr,
  924. int OffsetSize, laFloatGetF Get, laFloatSetF Set, int ArrayLength, laArrayGetLenF GetLen,
  925. laFloatArraySetF SetArr, laFloatArrayGetAllF GetAll, laFloatArraySetAllF SetAll, laFloatArraySetAllArrayF SetAllArr,
  926. laFloatReadF Read, laFloatArrayReadAllF ReadAll,
  927. u64bit Tag){
  928. laFloatProp *p = la_CreateProperty(Container, LA_PROP_FLOAT, Identifier, Name, Description, Prefix, Unit, DefaultWidget, Tag);
  929. if (ArrayLength > 1){
  930. p->Base.Len = ArrayLength;
  931. p->Base.PropertyType |= LA_PROP_ARRAY;
  932. }else if (GetLen)
  933. p->Base.GetLen = GetLen;
  934. p->Get = Get;
  935. p->Set = Set;
  936. p->SetAll = SetAll;
  937. p->GetAll = GetAll;
  938. p->SetAllArr = SetAllArr;
  939. p->SetArr = SetArr;
  940. p->DefArr = DefArr;
  941. p->DefVal = DefVal;
  942. p->Max = Max;
  943. p->Min = Min;
  944. p->Step = Step ? Step : 0.01;
  945. p->Base.Offset = OffsetSize;
  946. p->Base.OffsetIsPointer = (Tag&LA_UDF_REFER) ? 1 : 0;
  947. p->Read = Read;
  948. p->ReadAll = ReadAll;
  949. la_AssignPropertyGeneralSub(p);
  950. return p;
  951. }
  952. laProp *laAddEnumProperty(laPropContainer *Container, const char *Identifier, const char *Name, const char *Description, laWidget* DefaultWidget,
  953. const char *Prefix, const char *Unit, int DefVal, const int *DefArr,
  954. int OffsetSize, laEnumGetF Get, laEnumSetF Set, int ArrayLength, laArrayGetLenF GetLen,
  955. laEnumArraySetF SetArr, laEnumArrayGetAllF GetAll, laEnumArraySetAllF SetAll,
  956. laEnumReadF Read, laEnumArrayReadAllF ReadAll,
  957. u64bit Tag){
  958. laEnumProp *p = la_CreateProperty(Container, LA_PROP_ENUM, Identifier, Name, Description, Prefix, Unit, DefaultWidget, Tag);
  959. if (ArrayLength > 1){
  960. p->Base.Len = ArrayLength;
  961. p->Base.PropertyType |= LA_PROP_ARRAY;
  962. }else if (GetLen)
  963. p->Base.GetLen = GetLen;
  964. p->Get = Get;
  965. p->Set = Set;
  966. p->SetAll = SetAll;
  967. p->GetAll = GetAll;
  968. p->SetArr = SetArr;
  969. p->DefArr = DefArr;
  970. p->DefVal = DefVal;
  971. p->Base.Offset = OffsetSize;
  972. p->Base.OffsetIsPointer = (Tag&LA_UDF_REFER) ? 1 : 0;
  973. p->Read = Read;
  974. p->ReadAll = ReadAll;
  975. la_AssignPropertyGeneralSub(p);
  976. return p;
  977. }
  978. int laAddEnumItem(laProp *p, const char *Identifier, const char *Name, const char *Description, uint32_t IconID){
  979. laEnumItem *ei = memAcquire(sizeof(laEnumItem));
  980. laEnumProp *ep = p;
  981. ei->Identifier = Identifier;
  982. ei->Name = Name;
  983. ei->Description = Description;
  984. ei->IconID = IconID;
  985. ei->Index = ep->Items.pLast ? ((laEnumItem *)ep->Items.pLast)->Index + 1 : 0;
  986. lstAppendItem(&ep->Items, ei);
  987. return 1;
  988. }
  989. int laAddEnumItemAs(laProp *p, const char *Identifier, const char *Name, const char *Description, int Index, uint32_t IconID){
  990. laEnumItem *ei = memAcquire(sizeof(laEnumItem));
  991. laEnumProp *ep = p;
  992. ei->Identifier = Identifier;
  993. ei->Name = Name;
  994. ei->Description = Description;
  995. ei->IconID = IconID;
  996. ei->Index = Index;
  997. lstAppendItem(&ep->Items, ei);
  998. return 1;
  999. }
  1000. laProp *laAddStringProperty(laPropContainer *Container, const char *Identifier, const char *Name, const char *Description, laWidget* DefaultWidget,
  1001. const char *Prefix, const char *Unit, const char *DefStr,
  1002. int IsSafeString, int OffsetSize, laStringGetLenF GetLen, laStringGetF Get, laStringSetF Set,
  1003. laStringReadF Read,
  1004. u64bit Tag){
  1005. laStringProp *p = la_CreateProperty(Container, LA_PROP_STRING, Identifier, Name, Description, Prefix, Unit, DefaultWidget, Tag);
  1006. p->Get = Get;
  1007. p->Set = Set;
  1008. p->Getstrlen = GetLen;
  1009. p->DefStr = DefStr;
  1010. p->Base.Offset = OffsetSize;
  1011. p->Base.OffsetIsPointer = (Tag&LA_UDF_LOCAL) ? 0 : 1;
  1012. p->Read = Read;
  1013. p->IsSafeString = IsSafeString;
  1014. la_AssignPropertyGeneralSub(p);
  1015. return p;
  1016. }
  1017. laProp *laAddSubGroup(laPropContainer *Container, const char *Identifier, const char *Name, const char *Description,
  1018. const char *TargetId, laGetNodeTypeFunc GetType, laWidget* DefaultWidget, laUiDefineFunc DefaultUiDef,
  1019. int OffsetSize, laSubGetInstanceF Get, laSubGetInstanceF GetActive, laSubGetNextF GetNext, laSubSetInstanceF Set,
  1020. laSubGetStateF GetState, laSubSetStateF SetState, int ListHandleOffset, u64bit Tag){
  1021. laSubProp *p = la_CreateProperty(Container, LA_PROP_SUB, Identifier, Name, Description, 0, 0, DefaultWidget, Tag);
  1022. p->Base.Offset = OffsetSize;
  1023. p->Get = Get;
  1024. p->GetNext = GetNext;
  1025. p->GetActive = GetActive;
  1026. p->Set = Set;
  1027. p->GetState = GetState;
  1028. p->SetState = SetState;
  1029. p->TargetID = TargetId;
  1030. p->Base.OffsetIsPointer = (Tag & LA_UDF_LOCAL) ? 0 : 1;
  1031. p->Base.UiDefine = DefaultUiDef;
  1032. p->GetType = GetType;
  1033. p->ListHandleOffset = ListHandleOffset;
  1034. p->Base.UDFNoCreate = (Tag & LA_UDF_LOCAL) ? 1 : 0;
  1035. if (Tag & LA_UDF_SINGLE || (p->Base.OffsetIsPointer && !p->ListHandleOffset && !p->GetNext && !(Tag & LA_UDF_REFER))) p->Base.UDFIsSingle = 1;
  1036. la_AssignPropertyGeneralSub(p);
  1037. return p;
  1038. }
  1039. void laSubGroupExtraFunctions(laProp* p, laSubUIFilterF* UiFilter, laSubSaveFilterF* SaveFilter, laSubUIThemeF* GetTheme, laSubUIGapF GetGap, laSubUICategoryF GetCategory){
  1040. laSubProp *sp=p;
  1041. sp->UiFilter=UiFilter; sp->SaveFilter=SaveFilter; sp->GetTheme=GetTheme; sp->GetGap=GetGap; sp->GetCategory=GetCategory;
  1042. }
  1043. void laSubGroupDetachable(laProp *SubProp, laSubTypeDetachedGet DetachedGet, laSubTypeDetachedGetNext DetachedGetNext){
  1044. laSubProp *sp = SubProp;
  1045. SubProp->Tag |= LA_DETACHABLE;
  1046. sp->DetachedGet = DetachedGet;
  1047. sp->DetachedGetNext = DetachedGetNext;
  1048. }
  1049. laProp *laAddOperatorProperty(laPropContainer *Container, const char *Identifier, const char *Name, const char *Description,
  1050. const char *OperatorID, uint32_t IconID, laWidget* DefaultWidget){
  1051. laOperatorProp *p = la_CreateProperty(Container, LA_PROP_OPERATOR, Identifier, Name, Description, 0, 0, DefaultWidget, 0);
  1052. p->OperatorID = OperatorID;
  1053. p->Base.IconID = IconID;
  1054. la_AssignPropertyGeneralSub(p);
  1055. return p;
  1056. }
  1057. laProp *laAddRawProperty(laPropContainer *Container, const char *Identifier, const char *Name, const char *Description, int OffsetSize, laRawGetSizeF GetSize, laRawGetF RawGet, laRawSetF RawSet, u64bit Tag){
  1058. if(!RawGet && !GetSize) return 0;
  1059. laRawProp *p = la_CreateProperty(Container, LA_PROP_RAW, Identifier, Name, Description, 0, 0, 0, Tag);
  1060. p->Base.Offset = OffsetSize;
  1061. p->Base.OffsetIsPointer = (Tag & LA_UDF_LOCAL) ? 0 : 1;
  1062. p->RawGet=RawGet;
  1063. p->RawGetSize=GetSize;
  1064. p->RawSet=RawSet;
  1065. la_AssignPropertyGeneralSub(p);
  1066. return p;
  1067. }
  1068. void laRawPropertyExtraFunctions(laProp* p, laRawMultiGetF MultiGet, laRawMultiCanGetF CanMultiGet){
  1069. laRawProp *rp=p; rp->RawMultiCanGet=CanMultiGet; rp->RawMultiGet=MultiGet;
  1070. }
  1071. //void laPropertySignal(laProp* p, int Throw, int Catch) {
  1072. // p->SignalThrow = Throw;
  1073. // p->SignalCatch = Catch;
  1074. //}
  1075. int laIsPropertyReadOnly(laPropPack *pp){
  1076. if (pp && pp->LastPs && pp->LastPs->p->ReadOnly) return 1;
  1077. return 0;
  1078. }
  1079. int laGetPrefixP(laPropPack *p, char buf[8][64]){
  1080. char *prefix = transLate(p->LastPs->p->Prefix);
  1081. int i = 0, row = 0;
  1082. if (!prefix) return 0;
  1083. int len=strlen(prefix);
  1084. while ((prefix[i] != U'\0') && (i+=1+strGetStringTerminateBy(&prefix[i], U',', &buf[row]))){
  1085. row++; if(i>=len){break;}
  1086. }
  1087. return 1;
  1088. }
  1089. int laGetPrefix(laProp *p, char buf[8][64]){
  1090. char *prefix = transLate(p->Prefix);
  1091. int i = 0, row = 0;
  1092. if (!prefix) return 0;
  1093. while ((prefix[i] != U'\0') && (i += 1 + strGetStringTerminateBy(&prefix[i], U',', &buf[row]))) row++;
  1094. return 1;
  1095. }
  1096. laPropContainer* laGetInstanceType(laPropPack* pp, void* instance){
  1097. if(pp->LastPs->p->PropertyType != LA_PROP_SUB) return 0;
  1098. laSubProp* sp = pp->LastPs->p;
  1099. if(instance && sp->GetType){ return sp->GetType(instance); }
  1100. return pp->LastPs->p->SubProp;
  1101. }
  1102. laUiDefineFunc laGetPropertyUiDefine(laPropPack* pp, void* instance){
  1103. if(pp->LastPs->p->UiDefine) return pp->LastPs->p->UiDefine;
  1104. laPropContainer* pc=laGetInstanceType(pp, instance);
  1105. if(pc && pc->UiDefine) return pc->UiDefine;
  1106. return laui_SubPropInfoDefault;
  1107. }
  1108. laPropContainer* la_EnsureSubTarget(laSubProp* sp, void* optional_instance){
  1109. if(sp->Base.PropertyType!=LA_PROP_SUB){return sp->Base.SubProp;}
  1110. if(optional_instance && sp->GetType) return sp->GetType(optional_instance);
  1111. if(sp->Base.SubProp){ return sp->Base.SubProp; }
  1112. if(sp->TargetID)sp->Base.SubProp=la_ContainerLookup(sp->TargetID); return sp->Base.SubProp;
  1113. }
  1114. int laReadInt(laPropPack *pp, int n){
  1115. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1116. laIntProp *p = pp->LastPs->p;
  1117. if (p->Read) p->Read(pp->LastPs->UseInstance, n);
  1118. else
  1119. laSetInt(pp, n);
  1120. laNotifyUsersPP(pp);
  1121. return 1;
  1122. }
  1123. return 0;
  1124. }
  1125. int laSetInt(laPropPack *pp, int n){
  1126. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1127. laIntProp *p = pp->LastPs->p;
  1128. if (p->Max != p->Min){
  1129. n = n > p->Max ? p->Max : (n < p->Min ? p->Min : n);
  1130. }
  1131. if (p->Base.DetachedPP.LastPs){
  1132. p->Detached[0] = n;
  1133. laNotifyUsersPP(pp);
  1134. return 1;
  1135. }
  1136. if (p->Set) p->Set(pp->LastPs->UseInstance, n);
  1137. elif (pp->LastPs->p->Offset>=0){
  1138. if (!p->Base.ElementBytes || p->Base.ElementBytes == 4){
  1139. if (pp->LastPs->p->OffsetIsPointer) (**((int **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1140. else (*((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1141. }elif (p->Base.ElementBytes == 2){
  1142. if (pp->LastPs->p->OffsetIsPointer) (**((short **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1143. else (*((short *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1144. }elif (p->Base.ElementBytes == 1){
  1145. if (pp->LastPs->p->OffsetIsPointer) (**((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1146. else (*((BYTE *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1147. }
  1148. }
  1149. laNotifyUsersPP(pp);
  1150. return 1;
  1151. }
  1152. return 0;
  1153. }
  1154. int laGetInt(laPropPack *pp){
  1155. int n;
  1156. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1157. laIntProp *p = pp->LastPs->p;
  1158. if (p->Base.DetachedPP.LastPs){
  1159. return p->Detached[0];
  1160. }
  1161. if (!pp->LastPs->UseInstance) return 0;
  1162. if (((laIntProp *)pp->LastPs->p)->Get) return ((laIntProp *)pp->LastPs->p)->Get(pp->LastPs->UseInstance);
  1163. elif (pp->LastPs->p->Offset>=0){
  1164. if (!p->Base.ElementBytes || p->Base.ElementBytes == 4){
  1165. if (pp->LastPs->p->OffsetIsPointer) n = (**((int **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1166. else n = (*((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1167. }elif (p->Base.ElementBytes == 2){
  1168. if (pp->LastPs->p->OffsetIsPointer) n = (**((short **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1169. else n = (*((short *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1170. }elif (p->Base.ElementBytes == 1){
  1171. if (pp->LastPs->p->OffsetIsPointer) n = (**((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1172. else n = (*((BYTE *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1173. }
  1174. return n;
  1175. }
  1176. }
  1177. return 0;
  1178. }
  1179. int laReadFloat(laPropPack *pp, real n){
  1180. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1181. laFloatProp *p = pp->LastPs->p;
  1182. if (p->Read) p->Read(pp->LastPs->UseInstance, n);
  1183. else
  1184. laSetFloat(pp, n);
  1185. laNotifyUsersPP(pp);
  1186. return 1;
  1187. }
  1188. return 0;
  1189. }
  1190. int laSetFloat(laPropPack *pp, real n){
  1191. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1192. laFloatProp *p = pp->LastPs->p;
  1193. if (p->Max != p->Min){
  1194. n = n > p->Max ? p->Max : (n < p->Min ? p->Min : n);
  1195. }
  1196. if (p->Base.DetachedPP.LastPs){
  1197. p->Detached[0] = n;
  1198. laNotifyUsersPP(pp);
  1199. return 1;
  1200. }
  1201. if (p->Set) p->Set(pp->LastPs->UseInstance, n);
  1202. elif (pp->LastPs->p->Offset>=0){
  1203. if (pp->LastPs->p->OffsetIsPointer) (**((real **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1204. else (*((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1205. }
  1206. laNotifyUsersPP(pp);
  1207. return 1;
  1208. }
  1209. return 0;
  1210. }
  1211. real laGetFloat(laPropPack *pp){
  1212. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1213. laFloatProp *p = pp->LastPs->p;
  1214. if (p->Base.DetachedPP.LastPs){
  1215. return p->Detached[0];
  1216. }
  1217. if (!pp->LastPs->UseInstance) return 0;
  1218. if (((laFloatProp *)pp->LastPs->p)->Get) return ((laFloatProp *)pp->LastPs->p)->Get(pp->LastPs->UseInstance);
  1219. elif (pp->LastPs->p->Offset>=0){
  1220. if (pp->LastPs->p->OffsetIsPointer) return (**((real **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1221. else return (*((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1222. }
  1223. }
  1224. return 0;
  1225. }
  1226. int laGetArrayLength(laPropPack *pp){
  1227. if (!pp->LastPs->p) return 0;
  1228. if (pp->LastPs->p->GetLen) return pp->LastPs->p->GetLen(pp->LastPs->UseInstance);
  1229. else{
  1230. if(pp->LastPs->p->Len==0) pp->LastPs->p->Len=1;
  1231. return pp->LastPs->p->Len;
  1232. }
  1233. }
  1234. int laSetIntArraySingle(laPropPack *pp, int index, int n){
  1235. laIntProp *p = pp->LastPs->p;
  1236. if (p->Max != p->Min){
  1237. n = n > p->Max ? p->Max : (n < p->Min ? p->Min : n);
  1238. }
  1239. if (p->Base.DetachedPP.LastPs){
  1240. p->Detached[index] = n;
  1241. laNotifyUsersPP(pp);
  1242. return 1;
  1243. }
  1244. if (!pp->LastPs->UseInstance) return 0;
  1245. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1246. if (p->SetArr){
  1247. p->SetArr(pp->LastPs->UseInstance, index, n);
  1248. laNotifyUsersPP(pp);
  1249. return 1;
  1250. }elif(p->SetAllArr){
  1251. int len = laGetArrayLength(pp); int* t=malloc(len*sizeof(int));
  1252. laGetIntArray(pp, t); t[index]=n;
  1253. laSetIntArrayAllArray(pp, t); free(t);
  1254. return 1;
  1255. }elif(p->Set){
  1256. laSetInt(pp, n);
  1257. return 1;
  1258. }elif (pp->LastPs->p->Offset>=0){
  1259. int *src = (pp->LastPs->p->OffsetIsPointer) ? ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1260. switch(pp->LastPs->p->ElementBytes){
  1261. default: case 4: src[index] = n; break;
  1262. case 2: ((short*)src)[index]=n; break;
  1263. case 1: ((char*)src)[index]=n; break;
  1264. }
  1265. laNotifyUsersPP(pp); return 1;
  1266. }
  1267. }
  1268. return 0;
  1269. }
  1270. int laSetIntArrayAll(laPropPack *pp, int n){
  1271. laIntProp *p = pp->LastPs->p;
  1272. if (p->Max != p->Min){
  1273. n = n > p->Max ? p->Max : (n < p->Min ? p->Min : n);
  1274. }
  1275. if (p->Base.DetachedPP.LastPs){
  1276. int i = 0, len = laGetArrayLength(pp);
  1277. for (i; i < len; i++){
  1278. p->Detached[i] = n;
  1279. }
  1280. laNotifyUsersPP(pp);
  1281. return 1;
  1282. }
  1283. if (!pp->LastPs->UseInstance) return 0;
  1284. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1285. if (p->SetAll) p->SetAll(pp->LastPs->UseInstance, n);
  1286. else if (p->SetArr){
  1287. int i = 0, len = laGetArrayLength(pp);
  1288. for (i; i < len; i++){
  1289. p->SetArr(pp->LastPs->UseInstance, i, n);
  1290. }
  1291. }elif (pp->LastPs->p->Offset>=0){
  1292. int *src = (pp->LastPs->p->OffsetIsPointer) ? ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1293. int i = 0, len = laGetArrayLength(pp);
  1294. for (i; i < len; i++){
  1295. switch(pp->LastPs->p->ElementBytes){
  1296. default: case 4: src[i] = n; break;
  1297. case 2: ((short*)src)[i]=n; break;
  1298. case 1: ((char*)src)[i]=n; break;
  1299. }
  1300. }
  1301. }
  1302. laNotifyUsersPP(pp);
  1303. return 1;
  1304. }
  1305. return 0;
  1306. }
  1307. int laReadIntArrayAllArray(laPropPack *pp, int *arr){
  1308. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1309. if (((laIntProp *)pp->LastPs->p)->Base.DetachedPP.LastPs){
  1310. int i = 0, len = laGetArrayLength(pp);
  1311. for (i; i < len; i++){
  1312. ((laIntProp *)pp->LastPs->p)->Detached[i] = arr[i];
  1313. }
  1314. return 1;
  1315. }
  1316. if (!pp->LastPs->UseInstance) return 0;
  1317. if (((laIntProp *)pp->LastPs->p)->SetAllArr) ((laIntProp *)pp->LastPs->p)->SetAllArr(pp->LastPs->UseInstance, arr);
  1318. else
  1319. laSetIntArrayAllArray(pp, arr);
  1320. laNotifyUsersPP(pp);
  1321. return 1;
  1322. }
  1323. return 0;
  1324. }
  1325. int laSetIntArrayAllArray(laPropPack *pp, int *arr){
  1326. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1327. if (((laIntProp *)pp->LastPs->p)->Base.DetachedPP.LastPs){
  1328. int i = 0, len = laGetArrayLength(pp);
  1329. for (i; i < len; i++){
  1330. ((laIntProp *)pp->LastPs->p)->Detached[i] = arr[i];
  1331. }
  1332. laNotifyUsersPP(pp);
  1333. return 1;
  1334. }
  1335. if (!pp->LastPs->UseInstance) return 0;
  1336. if (((laIntProp *)pp->LastPs->p)->SetAllArr) ((laIntProp *)pp->LastPs->p)->SetAllArr(pp->LastPs->UseInstance, arr);
  1337. else if (((laIntProp *)pp->LastPs->p)->SetArr){
  1338. int i = 0, len = laGetArrayLength(pp);
  1339. for (i; i < len; i++){
  1340. ((laIntProp *)pp->LastPs->p)->SetArr(pp->LastPs->UseInstance, i, arr[i]);
  1341. }
  1342. }else if (pp->LastPs->p->Offset>=0){
  1343. int *src = (pp->LastPs->p->OffsetIsPointer) ? ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1344. int i = 0, len = laGetArrayLength(pp);
  1345. for (i; i < len; i++){
  1346. switch(pp->LastPs->p->ElementBytes){
  1347. default: case 4: src[i] =arr[i]; break;
  1348. case 2: ((short*)src)[i]=arr[i]; break;
  1349. case 1: ((char*)src)[i]=arr[i]; break;
  1350. }
  1351. }
  1352. }
  1353. laNotifyUsersPP(pp);
  1354. return 1;
  1355. }
  1356. return 0;
  1357. }
  1358. int laGetIntArray(laPropPack *pp, int *result){
  1359. if (pp->LastPs->p->PropertyType & LA_PROP_INT){
  1360. laIntProp *p = pp->LastPs->p;
  1361. if (p->Base.DetachedPP.LastPs){
  1362. int len = laGetArrayLength(pp);
  1363. memcpy(result, p->Detached, len * sizeof(int));
  1364. return 1;
  1365. }
  1366. if (!pp->LastPs->UseInstance) return 0;
  1367. if (!((laIntProp *)pp->LastPs->p)->GetAll){
  1368. int len = laGetArrayLength(pp);
  1369. if(len==1){ *result=laGetInt(pp); return 1; }
  1370. if(pp->LastPs->p->Offset>=0){
  1371. int *src = (pp->LastPs->p->OffsetIsPointer) ? ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1372. switch(pp->LastPs->p->ElementBytes){
  1373. default: case 4: memcpy(result, src, len * sizeof(int)); break;
  1374. case 2: for(int i=0;i<len;i++){ result[i]=((short*)src)[i]; } break;
  1375. case 1: for(int i=0;i<len;i++){ result[i]=((char*)src)[i]; } break;
  1376. }
  1377. }else{ *result=0; }
  1378. return 1;
  1379. }else{
  1380. ((laIntProp *)pp->LastPs->p)->GetAll(pp->LastPs->UseInstance, result);
  1381. return 1;
  1382. }
  1383. }
  1384. return 0;
  1385. }
  1386. int laSetFloatArraySingle(laPropPack *pp, int index, real n){
  1387. laFloatProp *p = pp->LastPs->p;
  1388. if (p->Max != p->Min){
  1389. n = n > p->Max ? p->Max : (n < p->Min ? p->Min : n);
  1390. }
  1391. if (p->Base.DetachedPP.LastPs){
  1392. p->Detached[index] = n;
  1393. laNotifyUsersPP(pp);
  1394. return 1;
  1395. }
  1396. if (!pp->LastPs->UseInstance) return 0;
  1397. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1398. if (p->SetArr){
  1399. p->SetArr(pp->LastPs->UseInstance, index, n);
  1400. laNotifyUsersPP(pp);
  1401. return 1;
  1402. }elif(p->SetAllArr){
  1403. int len = laGetArrayLength(pp); real* t=malloc(len*sizeof(real));
  1404. laGetFloatArray(pp, t); t[index]=n;
  1405. laSetFloatArrayAllArray(pp, t); free(t);
  1406. return 1;
  1407. }elif(p->Set){
  1408. laSetFloat(pp, n);
  1409. return 1;
  1410. }elif (pp->LastPs->p->Offset>=0){
  1411. real *src = (pp->LastPs->p->OffsetIsPointer) ? ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1412. src[index] = n;
  1413. laNotifyUsersPP(pp);
  1414. return 1;
  1415. }
  1416. }
  1417. return 0;
  1418. }
  1419. int laSetFloatArrayAll(laPropPack *pp, real n){
  1420. laFloatProp *p = pp->LastPs->p;
  1421. if (p->Max != p->Min){
  1422. n = n > p->Max ? p->Max : (n < p->Min ? p->Min : n);
  1423. }
  1424. if (p->Base.DetachedPP.LastPs){
  1425. int i = 0, len = laGetArrayLength(pp);
  1426. for (i; i < len; i++){
  1427. p->Detached[i] = n;
  1428. }
  1429. laNotifyUsersPP(pp);
  1430. return 1;
  1431. }
  1432. if (!pp->LastPs->UseInstance) return 0;
  1433. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1434. if (p->SetAll) p->SetAll(pp->LastPs->UseInstance, n);
  1435. else if (p->SetArr){
  1436. int i = 0, len = laGetArrayLength(pp);
  1437. for (i; i < len; i++){
  1438. p->SetArr(pp->LastPs->UseInstance, i, n);
  1439. }
  1440. }else if (pp->LastPs->p->Offset>=0){
  1441. real *src = (pp->LastPs->p->OffsetIsPointer) ? ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1442. int i = 0, len = laGetArrayLength(pp);
  1443. for (i; i < len; i++){ src[i] = n; }
  1444. }
  1445. laNotifyUsersPP(pp);
  1446. return 1;
  1447. }
  1448. return 0;
  1449. }
  1450. int laReadFloatArrayAllArray(laPropPack *pp, real *arr){
  1451. if (((laFloatProp *)pp->LastPs->p)->Base.DetachedPP.LastPs){
  1452. int i = 0, len = laGetArrayLength(pp);
  1453. for (i; i < len; i++){
  1454. ((laFloatProp *)pp->LastPs->p)->Detached[i] = arr[i];
  1455. }
  1456. return 1;
  1457. }
  1458. if (!pp->LastPs->UseInstance) return 0;
  1459. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1460. if (((laFloatProp *)pp->LastPs->p)->ReadAll) ((laFloatProp *)pp->LastPs->p)->ReadAll(pp->LastPs->UseInstance, arr);
  1461. else if (((laFloatProp *)pp->LastPs->p)->SetArr){
  1462. int i = 0, len = laGetArrayLength(pp);
  1463. for (i; i < len; i++){
  1464. ((laFloatProp *)pp->LastPs->p)->SetArr(pp->LastPs->UseInstance, i, arr[i]);
  1465. }
  1466. }else if (pp->LastPs->p->Offset>=0){
  1467. real *src = (pp->LastPs->p->OffsetIsPointer) ? ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1468. int i = 0, len = laGetArrayLength(pp);
  1469. for (i; i < len; i++){ src[i] = arr[i]; }
  1470. }
  1471. laNotifyUsersPP(pp);
  1472. return 1;
  1473. }
  1474. return 0;
  1475. }
  1476. int laSetFloatArrayAllArray(laPropPack *pp, real *arr){
  1477. if (((laFloatProp *)pp->LastPs->p)->Base.DetachedPP.LastPs){
  1478. int i = 0, len = laGetArrayLength(pp);
  1479. for (i; i < len; i++){
  1480. ((laFloatProp *)pp->LastPs->p)->Detached[i] = arr[i];
  1481. }
  1482. laNotifyUsersPP(pp);
  1483. return 1;
  1484. }
  1485. if (!pp->LastPs->UseInstance) return 0;
  1486. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1487. if (((laFloatProp *)pp->LastPs->p)->SetAllArr) ((laFloatProp *)pp->LastPs->p)->SetAllArr(pp->LastPs->UseInstance, arr);
  1488. else if (((laFloatProp *)pp->LastPs->p)->SetArr){
  1489. int i = 0, len = laGetArrayLength(pp);
  1490. for (i; i < len; i++){
  1491. ((laFloatProp *)pp->LastPs->p)->SetArr(pp->LastPs->UseInstance, i, arr[i]);
  1492. }
  1493. }else if (pp->LastPs->p->Offset>=0){
  1494. real *src = (pp->LastPs->p->OffsetIsPointer) ? ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1495. int i = 0, len = laGetArrayLength(pp);
  1496. for (i; i < len; i++){ src[i] = arr[i]; }
  1497. }
  1498. laNotifyUsersPP(pp);
  1499. return 1;
  1500. }
  1501. return 0;
  1502. }
  1503. int laGetFloatArray(laPropPack *pp, real *result){
  1504. if (pp->LastPs->p->PropertyType & LA_PROP_FLOAT){
  1505. laFloatProp *p = pp->LastPs->p;
  1506. if (p->Base.DetachedPP.LastPs){
  1507. int len = laGetArrayLength(pp);
  1508. memcpy(result, p->Detached, len * sizeof(real));
  1509. return 1;
  1510. }
  1511. if (!pp->LastPs->UseInstance) return 0;
  1512. if (!((laFloatProp *)pp->LastPs->p)->GetAll){
  1513. int len = laGetArrayLength(pp);
  1514. if(len==1){ *result=laGetFloat(pp); return 1; }
  1515. if(pp->LastPs->p->Offset>=0){
  1516. real *src = (pp->LastPs->p->OffsetIsPointer) ? ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) : ((real *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1517. memcpy(result, src, len * sizeof(real));
  1518. }else{ *result=0; }
  1519. return 1;
  1520. }else{
  1521. ((laFloatProp *)pp->LastPs->p)->GetAll(pp->LastPs->UseInstance, result);
  1522. return 1;
  1523. }
  1524. }
  1525. return 0;
  1526. }
  1527. int laReadEnum(laPropPack *pp, int n){
  1528. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1529. if (((laEnumProp *)pp->LastPs->p)->Read) ((laEnumProp *)pp->LastPs->p)->Read(pp->LastPs->UseInstance, n);
  1530. else laSetEnum(pp, n);
  1531. laNotifyUsersPP(pp);
  1532. return 1;
  1533. }
  1534. return 0;
  1535. }
  1536. int laSetEnumExternal(laPropPack *pp, int n){
  1537. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1538. laEnumProp *ep = pp->LastPs->p;
  1539. laEnumItem *ei;
  1540. int i = 0;
  1541. for (ei = ep->Items.pFirst; ei; ei = ei->Item.pNext){
  1542. if (i == n) break; i++;
  1543. }
  1544. laSetEnum(pp, ei->Index);
  1545. }
  1546. return 0;
  1547. }
  1548. int laSetEnum(laPropPack *pp, int n){
  1549. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1550. laEnumProp *p = pp->LastPs->p;
  1551. if (p->Base.DetachedPP.LastPs){
  1552. p->Detached[0] = n;
  1553. laNotifyUsersPP(pp);
  1554. return 1;
  1555. }
  1556. if (!pp->LastPs->UseInstance) return 0;
  1557. if (p->Set) p->Set(pp->LastPs->UseInstance, n);
  1558. elif(p->Base.Offset>=0) {
  1559. if (!p->Base.ElementBytes || p->Base.ElementBytes == 4){
  1560. if (pp->LastPs->p->OffsetIsPointer) (**((int **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1561. else (*((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1562. }elif (p->Base.ElementBytes == 2){
  1563. if (pp->LastPs->p->OffsetIsPointer) (**((short **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1564. else (*((short *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1565. }elif (p->Base.ElementBytes == 1){
  1566. if (pp->LastPs->p->OffsetIsPointer) (**((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1567. else (*((BYTE *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset))) = n;
  1568. }
  1569. }
  1570. laNotifyUsersPP(pp);
  1571. return 1;
  1572. }
  1573. return 0;
  1574. }
  1575. int laReadEnumArrayAll(laPropPack *pp, int *n){
  1576. int i, len = pp->LastPs->p->Len;
  1577. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1578. if (((laEnumProp *)pp->LastPs->p)->ReadAll) ((laEnumProp *)pp->LastPs->p)->ReadAll(pp->LastPs->UseInstance, n);
  1579. else
  1580. for (i = 0; i < len; i++){
  1581. laSetEnumArrayIndexed(pp, i, n[i]);
  1582. }
  1583. laNotifyUsersPP(pp);
  1584. return 1;
  1585. }
  1586. return 0;
  1587. }
  1588. int laSetEnumArrayIndexedExternal(laPropPack *pp, int index, int n){
  1589. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1590. laEnumProp *ep = pp->LastPs->p;
  1591. laEnumItem *ei;
  1592. int i = 0;
  1593. for (ei = ep->Items.pFirst; ei; ei = ei->Item.pNext){
  1594. if (i == n) break;
  1595. i++;
  1596. }
  1597. laSetEnumArrayIndexed(pp, index,ei->Index);
  1598. }
  1599. return 0;
  1600. }
  1601. int laSetEnumArrayIndexed(laPropPack *pp, int index, int n){
  1602. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1603. laEnumProp *p = pp->LastPs->p;
  1604. if (p->Base.DetachedPP.LastPs){
  1605. p->Detached[index] = n;
  1606. laNotifyUsersPP(pp);return 1;
  1607. }
  1608. if (!pp->LastPs->UseInstance) return 0;
  1609. if (p->SetArr) p->SetArr(pp->LastPs->UseInstance, index, n);
  1610. elif(p->Set && index==0){ laSetEnum(pp, n); }
  1611. elif(p->Base.Offset>=0){
  1612. if (!p->Base.ElementBytes || p->Base.ElementBytes == 4){
  1613. if (pp->LastPs->p->OffsetIsPointer) (**((int **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + sizeof(int) * index))) = n;
  1614. else (*((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + sizeof(int) * index))) = n;
  1615. }elif (p->Base.ElementBytes == 2){
  1616. if (pp->LastPs->p->OffsetIsPointer) (**((short **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + sizeof(short) * index))) = n;
  1617. else (*((short *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + sizeof(short) * index))) = n;
  1618. }elif (p->Base.ElementBytes == 1){
  1619. if (pp->LastPs->p->OffsetIsPointer) (**((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + sizeof(char) * index))) = n;
  1620. else (*((BYTE *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + sizeof(char) * index))) = n;
  1621. }
  1622. }
  1623. laNotifyUsersPP(pp);
  1624. return 1;
  1625. }
  1626. return 0;
  1627. }
  1628. int laSetEnumArrayAllArray(laPropPack* pp, laEnumItem** ei){
  1629. int len=laGetArrayLength(pp);
  1630. for(int i=0;i<len;i++){
  1631. laSetEnumArrayIndexed(pp,i,ei[i]->Index);
  1632. }
  1633. return 0;
  1634. }
  1635. int laSetEnumArrayAll(laPropPack* pp, int EnumN){
  1636. int len=laGetArrayLength(pp);
  1637. laEnumProp *ep = pp->LastPs->p; laEnumItem *ei;
  1638. int i = 0; for (ei = ep->Items.pFirst; ei; ei = ei->Item.pNext){ if (i == EnumN) break; i++; }
  1639. for(int i=0;i<len;i++){
  1640. laSetEnumArrayIndexed(pp,i,ei->Index);
  1641. }
  1642. return 0;
  1643. }
  1644. laEnumItem *laGetEnum(laPropPack *pp){
  1645. int n;
  1646. laEnumItem *ei;
  1647. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1648. laEnumProp *p = pp->LastPs->p;
  1649. if (p->Base.DetachedPP.LastPs){
  1650. n = p->Detached[0];
  1651. }elif (!pp->LastPs->UseInstance) return 0;
  1652. elif (p->Get) n = p->Get(pp->LastPs->UseInstance);
  1653. elif(p->Base.Offset>=0){
  1654. if (!pp->LastPs->UseInstance) n = 0;
  1655. elif (!p->Base.ElementBytes || p->Base.ElementBytes == 4){
  1656. if (pp->LastPs->p->OffsetIsPointer) n = (**((int **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1657. else n = (*((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1658. }elif (p->Base.ElementBytes == 2){
  1659. if (pp->LastPs->p->OffsetIsPointer) n = (**((short **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1660. else n = (*((short *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1661. }elif (p->Base.ElementBytes == 1){
  1662. if (pp->LastPs->p->OffsetIsPointer) n = (**((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1663. else n = (*((BYTE *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)));
  1664. }
  1665. }
  1666. for (ei = p->Items.pFirst; ei; ei = ei->Item.pNext){
  1667. if (n == ei->Index) return ei;
  1668. }
  1669. }
  1670. return ((laEnumProp *)pp->LastPs->p)->Items.pFirst;
  1671. }
  1672. laEnumItem *laGetEnumArrayIndexed(laPropPack *pp, int index){
  1673. int n;
  1674. int i = 0;
  1675. laEnumItem *ei;
  1676. int result[16];
  1677. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1678. laEnumProp *p = pp->LastPs->p;
  1679. if (p->Base.DetachedPP.LastPs){
  1680. n = p->Detached[index];
  1681. }elif (!p->GetAll){
  1682. if(laGetArrayLength(pp)==1){return laGetEnum(pp);}
  1683. if(p->Base.Offset>=0){
  1684. if (!pp->LastPs->UseInstance) n = 0;
  1685. elif (!p->Base.ElementBytes || p->Base.ElementBytes == 4){
  1686. if (pp->LastPs->p->OffsetIsPointer) n = (**((int **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) + index * sizeof(int));
  1687. else n = (*((int *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + index * sizeof(int))));
  1688. }elif (p->Base.ElementBytes == 2){
  1689. if (pp->LastPs->p->OffsetIsPointer) n = (**((short **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) + index * sizeof(short));
  1690. else n = (*((short *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + index * sizeof(short))));
  1691. }elif (p->Base.ElementBytes == 1){
  1692. if (pp->LastPs->p->OffsetIsPointer) n = (**((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset)) + index * sizeof(char));
  1693. else n = (*((BYTE *)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset + index * sizeof(char))));
  1694. }
  1695. }
  1696. }else{
  1697. if (!pp->LastPs->UseInstance) n = 0;
  1698. else{
  1699. p->GetAll(pp->LastPs->UseInstance, result);
  1700. n = result[index];
  1701. }
  1702. }
  1703. for (ei = p->Items.pFirst; ei; ei = ei->Item.pNext){
  1704. if (n == ei->Index) return ei;
  1705. }
  1706. }
  1707. return 0;
  1708. }
  1709. int laGetEnumArray(laPropPack *pp, laEnumItem **result){
  1710. int len = laGetArrayLength(pp);
  1711. int i = 0;
  1712. laEnumItem *ei;
  1713. int Results[16];
  1714. laEnumProp *p = pp->LastPs->p;
  1715. if (p->Base.DetachedPP.LastPs){
  1716. for (int i = 0; i < len; i++){
  1717. Results[i] = p->Detached[i];
  1718. for (ei = ((laEnumProp *)pp->LastPs->p)->Items.pFirst; ei; ei = ei->Item.pNext){
  1719. if (ei->Index == Results[i]){result[i] = ei; break;}
  1720. }
  1721. }
  1722. }elif (!((laEnumProp *)pp->LastPs->p)->GetAll){
  1723. for (i; i < len; i++){
  1724. result[i] = laGetEnumArrayIndexed(pp, i);
  1725. }
  1726. return 1;
  1727. }else{
  1728. int a = 0;
  1729. ((laEnumProp *)pp->LastPs->p)->GetAll(pp->LastPs->UseInstance, Results);
  1730. for (i = 0; i < len; i++){
  1731. for (ei = ((laEnumProp *)pp->LastPs->p)->Items.pFirst; ei; ei = ei->Item.pNext){
  1732. if (ei->Index == Results[i]){ result[i] = ei; break; }
  1733. }
  1734. }
  1735. }
  1736. return 0;
  1737. }
  1738. laEnumItem *laGetEnumFromIdentifier(laEnumProp *p, char *Identifier){
  1739. laEnumItem *ei;
  1740. for (ei = p->Items.pFirst; ei; ei = ei->Item.pNext){
  1741. if (!strcmp(ei->Identifier, Identifier)) return ei;
  1742. }
  1743. return p->Items.pFirst;
  1744. }
  1745. int laEnumHasIcon(laPropPack *pp){
  1746. laEnumProp *p = pp->LastPs->p;
  1747. laEnumItem *ei;
  1748. if ((p->Base.PropertyType & LA_PROP_ENUM) != LA_PROP_ENUM) return 0;
  1749. for (ei = p->Items.pFirst; ei; ei = ei->Item.pNext){
  1750. if (ei->IconID) return 1;
  1751. }
  1752. return 0;
  1753. }
  1754. int laGetEnumEntryLen(laPropPack *pp){
  1755. int i = 0;
  1756. laEnumItem *ei;
  1757. if (pp->LastPs->p->PropertyType & LA_PROP_ENUM){
  1758. if (((laEnumProp *)pp->LastPs->p)->GetLen) return ((laEnumProp *)pp->LastPs->p)->GetLen(pp->LastPs->UseInstance);
  1759. for (ei = ((laEnumProp *)pp->LastPs->p)->Items.pFirst; ei; ei = ei->Item.pNext){
  1760. i += 1;
  1761. }
  1762. return i;
  1763. }
  1764. return 0;
  1765. }
  1766. int laReadString(laPropPack *pp, char *str){
  1767. if (pp->LastPs->p->PropertyType == LA_PROP_STRING){
  1768. if (((laStringProp *)pp->LastPs->p)->Read) ((laStringProp *)pp->LastPs->p)->Read(pp->LastPs->UseInstance, str);
  1769. else
  1770. laSetString(pp, str);
  1771. laNotifyUsersPP(pp);
  1772. return 1;
  1773. }
  1774. return 0;
  1775. }
  1776. int laSetString(laPropPack *pp, char *str){
  1777. char **add = 0;
  1778. if (pp->LastPs->p->PropertyType == LA_PROP_STRING){
  1779. laStringProp *p = pp->LastPs->p;
  1780. if (p->Base.DetachedPP.LastPs){
  1781. strCopyFull(p->Detached, str);
  1782. laNotifyUsersPP(pp);
  1783. return 1;
  1784. }
  1785. if (p->Set){
  1786. p->Set(pp->LastPs->UseInstance, str);
  1787. laNotifyUsersPP(pp);
  1788. return 1;
  1789. }
  1790. if (!pp->LastPs->UseInstance) return 0;
  1791. if (p->IsSafeString){
  1792. add = ((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1793. strSafeSet(add, str);
  1794. laNotifyUsersPP(pp);
  1795. return 1;
  1796. }
  1797. if (p->Base.Offset){
  1798. if (p->Base.OffsetIsPointer){
  1799. add = ((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1800. if (!(*add)) return 0;
  1801. strCopyFull(*add, str);
  1802. }else strCopyFull(((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset), str);
  1803. }
  1804. laNotifyUsersPP(pp);
  1805. return 1;
  1806. }
  1807. return 0;
  1808. }
  1809. int laGetString(laPropPack *pp, char *result, char** direct_result){
  1810. if (pp->LastPs->p->PropertyType == LA_PROP_STRING){
  1811. laStringProp *p = pp->LastPs->p;
  1812. if (p->Base.DetachedPP.LastPs){
  1813. strCopyFull(result, p->Detached);
  1814. return 1;
  1815. }
  1816. if (!pp->LastPs->UseInstance) return 0;
  1817. if (p->Get){ p->Get(pp->LastPs->UseInstance, result, direct_result); return 1; }
  1818. if (pp->LastPs->p->Offset>=0){
  1819. if (p->IsSafeString){
  1820. laSafeString **add = ((laSafeString **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1821. //printf(" -- %s\n",p->Base.Identifier);
  1822. if ((!pp->LastPs->UseInstance) || (!*add)) return 0;
  1823. if((*add)->Ptr){ *direct_result = (*add)->Ptr; return 1;}
  1824. return 0;
  1825. }else{
  1826. if (pp->LastPs->p->OffsetIsPointer) *direct_result=*((char **)((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset));
  1827. else *direct_result=((BYTE *)pp->LastPs->UseInstance + pp->LastPs->p->Offset);
  1828. }
  1829. }
  1830. return 1;
  1831. }
  1832. return 0;
  1833. }
  1834. void *laGetInstance(laProp *sub, void *FromInstance, laPropIterator *Iter){
  1835. laSubProp *sp = sub;
  1836. laPropIterator IT = {0};
  1837. if (!Iter) Iter = &IT;
  1838. if (Iter) Iter->Parent = FromInstance;
  1839. if (sub->PropertyType == LA_PROP_SUB){
  1840. if (sp->Base.DetachedPP.LastPs){ if(sp->DetachedGet) return sp->DetachedGet(0, Iter);
  1841. return sp->Detached;
  1842. }
  1843. if (!FromInstance) return 0;
  1844. if (sp->Get) return sp->Get(FromInstance, Iter);
  1845. else if (sp->ListHandleOffset){
  1846. void *address = *((u64bit*)((BYTE *)FromInstance + sp->ListHandleOffset));
  1847. return address;
  1848. }else return laGetActiveInstance(sub, FromInstance, Iter);
  1849. }
  1850. return 0;
  1851. }
  1852. void *laGetNextInstance(laProp *sub, void *ThisInstance, laPropIterator *Iter){
  1853. laSubProp *sp = sub;
  1854. if (sub->PropertyType == LA_PROP_SUB){
  1855. if (sp->Base.DetachedPP.LastPs && sp->DetachedGetNext) return sp->DetachedGetNext(ThisInstance, Iter);
  1856. if (!ThisInstance) return 0;
  1857. if (sp->GetNext) return (sp->GetNext(ThisInstance, Iter));
  1858. else if (sp->ListHandleOffset){
  1859. laListItem *li = ThisInstance;
  1860. return li->pNext;
  1861. }
  1862. }
  1863. return 0;
  1864. }
  1865. void *laGetActiveInstanceStrict(laProp *sub, void *FromInstance){
  1866. laSubProp *sp = sub;
  1867. if (sub->PropertyType == LA_PROP_SUB){
  1868. if (sp->Base.DetachedPP.LastPs){
  1869. return sp->Detached;
  1870. }
  1871. if (!FromInstance) return 0;
  1872. if (sp->GetActive) return sp->GetActive(FromInstance, 0);
  1873. if (sub->Offset>=0){
  1874. if (sub->OffsetIsPointer /*&& (sp->Get||sp->ListHandleOffset)*/){
  1875. void **a = (void **)((BYTE *)FromInstance + sub->Offset); return *a;
  1876. }else return ((BYTE *)FromInstance + sub->Offset);
  1877. }
  1878. }
  1879. return 0; // laGetInstance(sub, FromInstance, 0);
  1880. }
  1881. void *laGetActiveInstance(laProp *sub, void *FromInstance, laPropIterator *Iter){
  1882. laSubProp *sp = sub;
  1883. if (sub->PropertyType == LA_PROP_SUB){
  1884. if (sp->Base.DetachedPP.LastPs){
  1885. return sp->Detached;
  1886. }
  1887. if (!FromInstance) return 0;
  1888. if (sp->GetActive) return sp->GetActive(FromInstance, Iter);
  1889. if (sp->Get) return laGetInstance(sub, FromInstance, Iter);
  1890. if (sub->Offset>=0){
  1891. if (sub->OffsetIsPointer){
  1892. void **a = (void **)((BYTE *)FromInstance + sub->Offset); return *a;
  1893. }else return ((BYTE *)FromInstance + sub->Offset);
  1894. }
  1895. }
  1896. return 0;
  1897. }
  1898. void laSetActiveInstance(laProp *sub, void *FromInstance, void *Instance){
  1899. laSubProp *sp = sub;
  1900. if (sub->PropertyType == LA_PROP_SUB){
  1901. MAIN._CONTAINER_SETTING=sub->Container;
  1902. MAIN._PROP_SETTING=sub;
  1903. if (sp->Base.DetachedPP.LastPs){
  1904. memAssignRef(sp, &sp->Detached, Instance);
  1905. laNotifySubPropUsers(sp, FromInstance);
  1906. return;
  1907. }
  1908. if (!FromInstance) return;
  1909. if (sp->SetState){
  1910. laNotifySubPropUsers(sp, FromInstance);
  1911. sp->SetState(FromInstance, Instance, LA_UI_ACTIVE);
  1912. }
  1913. if (sp->Set){
  1914. laNotifySubPropUsers(sp, FromInstance);
  1915. sp->Set(FromInstance, Instance);
  1916. }
  1917. if (sub->OffsetIsPointer && sub->Offset>=0){
  1918. void **a = (void **)((BYTE *)FromInstance + sub->Offset);
  1919. laNotifySubPropUsers(sp, FromInstance);
  1920. memAssignRefSafe(sub->Container->Hyper?sp:0,FromInstance, a, Instance);
  1921. }
  1922. }
  1923. return;
  1924. }
  1925. void laAppendInstance(laSubProp *sub, void *FromInstance, void *Instance){
  1926. if (sub->ListHandleOffset){
  1927. lstAppendItem((BYTE *)FromInstance + sub->ListHandleOffset, Instance);
  1928. }
  1929. }
  1930. void* laGetRaw(laPropPack *pp, int* r_size, int* return_is_a_copy){
  1931. if (pp->LastPs->p->PropertyType == LA_PROP_RAW){
  1932. laRawProp* rp=pp->LastPs->p;
  1933. if(rp->RawGet){ return rp->RawGet(pp->LastPs->UseInstance, r_size, return_is_a_copy); }
  1934. int s=rp->RawGetSize(pp->LastPs->UseInstance); void* data=0;
  1935. if(rp->Base.OffsetIsPointer){ data=*((void**)(((char*)pp->LastPs->UseInstance)+rp->Base.Offset)); }
  1936. else{ data=((char*)pp->LastPs->UseInstance)+rp->Base.Offset; }
  1937. *return_is_a_copy=0; if(r_size) *r_size=s;
  1938. return data;
  1939. } return 0;
  1940. }
  1941. int laGetMultiRaw(laPropPack* pp, int* r_chunks, int* r_sizes, void** pointers){
  1942. if (pp->LastPs->p->PropertyType == LA_PROP_RAW){
  1943. laRawProp* rp=pp->LastPs->p; if(!rp->RawMultiGet){ return 0; }
  1944. rp->RawMultiGet(pp->LastPs->UseInstance,r_chunks,r_sizes,pointers);
  1945. if(*r_chunks>0){ return 1; } return 0;
  1946. }
  1947. return 0;
  1948. }
  1949. int laSetRaw(laPropPack *pp, void* data, uint32_t _size){
  1950. if (pp->LastPs->p->PropertyType == LA_PROP_RAW){ laRawProp* rp=pp->LastPs->p;
  1951. if(rp->RawSet){ rp->RawSet(pp->LastPs->UseInstance, data, _size); return 1; }
  1952. if(rp->Base.OffsetIsPointer){ void** target=(((char*)pp->LastPs->UseInstance)+rp->Base.Offset); if(*target) free(*target);
  1953. void* newd=_size?calloc(1,_size):0; if(_size)memcpy(newd, data, _size); (*target)=newd;
  1954. }
  1955. else{ memcpy(((char*)pp->LastPs->UseInstance)+rp->Base.Offset,data,_size); }
  1956. return 1;
  1957. } return 0;
  1958. }
  1959. int laTryGetInstanceIdentifier(void* Instance, laPropContainer* pc, char* identifier, char** here){
  1960. if(!Instance || !pc) return 0;
  1961. laProp* p=la_PropLookupIdentifierItem(&pc->Props);
  1962. if(p){
  1963. laPropPack PP={0}; laPropStep PS={0};
  1964. PS.p=p; PS.Type='.'; PS.UseInstance=Instance; PP.LastPs=&PS; PP.EndInstance=Instance;
  1965. if(p->PropertyType==LA_PROP_STRING){ laGetString(&PP,identifier,here); }
  1966. elif(p->PropertyType&LA_PROP_INT){ int r[16]={0}; laGetIntArray(&PP,r); sprintf("(%s=%d)",p->Identifier,r[0]); }
  1967. elif(p->PropertyType&LA_PROP_FLOAT){ real r[16]={0}; laGetFloatArray(&PP,r); sprintf("(%s=%lf)",p->Identifier,r[0]); }
  1968. elif(p->PropertyType&LA_PROP_ENUM){ laEnumItem *r[16]={0}; laGetEnumArray(&PP,r); sprintf("(%s=%lf)",p->Identifier,r[0]?r[0]->Identifier:"?"); }
  1969. else{ sprintf("(%.6x)",identifier,Instance); }
  1970. return 1;
  1971. }
  1972. return 0;
  1973. }
  1974. int laGetIntRange(laPropPack *pp, int *min, int *max){
  1975. laIntProp *p = pp->LastPs->p;
  1976. if (p->Max != p->Min){
  1977. *min = p->Min;
  1978. *max = p->Max;
  1979. return 1;
  1980. }
  1981. return 0;
  1982. }
  1983. int laGetFloatRange(laPropPack *pp, real *min, real *max){
  1984. laFloatProp *p = pp->LastPs->p;
  1985. if (p->Max != p->Min){
  1986. *min = p->Min;
  1987. *max = p->Max;
  1988. return 1;
  1989. }
  1990. return 0;
  1991. }
  1992. int laCanGetMultiRaw(laProp *p,void* inst){
  1993. laRawProp *rp = p; if (p->PropertyType == LA_PROP_RAW){
  1994. if (rp->RawMultiGet && (!rp->RawMultiCanGet || rp->RawMultiCanGet(inst))) return 1; } return 0;
  1995. }
  1996. int laCanGetState(laProp *sub){ laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetState) return 1; } return 0; }
  1997. int laCanGetTheme(laProp *sub){ laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetTheme) return 1; } return 0; }
  1998. int laCanGetGap(laProp *sub){ laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetGap) return 1; } return 0; }
  1999. int laCanGetCategory(laProp *sub){ laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetCategory) return 1; } return 0; }
  2000. int laGetUiState(laProp *sub, void *Instance){
  2001. laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetState) return sp->GetState(Instance); } return 0;
  2002. }
  2003. laTheme* laGetUiTheme(laProp *sub, void* parent, void *Instance){
  2004. laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetTheme) return sp->GetTheme(parent,Instance); } return 0;
  2005. }
  2006. int laGetUiGap(laProp *sub, void* parent, void *Instance){
  2007. laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetGap) return sp->GetGap(parent,Instance); } return 0;
  2008. }
  2009. void laGetCategory(laProp *sub, void* parent, void *Instance, char* buf, char** buf_ptr){
  2010. laSubProp *sp = sub; if (sub->PropertyType == LA_PROP_SUB){ if (sp->GetCategory) sp->GetCategory(parent,Instance, buf, buf_ptr); }
  2011. }
  2012. int laSetState(laProp *sub, void *FromInstance, void *Instance, int State){
  2013. laSubProp *sp = sub;
  2014. if (sub->PropertyType == LA_PROP_SUB){
  2015. if (sp->SetState){
  2016. laNotifySubPropUsers(sub, FromInstance);
  2017. return sp->SetState(FromInstance, Instance, State);
  2018. }
  2019. }
  2020. return 0;
  2021. }
  2022. int laActuateProp(laPropPack *This, laPropPack *RunPP, laOperator *OptionalFrom, laEvent *e){
  2023. if (RunPP->LastPs->p->PropertyType & LA_PROP_OPERATOR){
  2024. if (!((laOperatorProp *)RunPP->LastPs->p)->OperatorType){
  2025. ((laOperatorProp *)RunPP->LastPs->p)->OperatorType = laGetOperatorType(((laOperatorProp *)RunPP->LastPs->p)->OperatorID);
  2026. }
  2027. return laInvokeP(OptionalFrom, ((laOperatorProp *)RunPP->LastPs->p)->OperatorType, e, This, 0, 0); //ARGS
  2028. }
  2029. return LA_CANCELED;
  2030. }
  2031. void laMarkPropChanged(laPropPack* pp){
  2032. if(!pp) return;
  2033. if(!pp->Go){ if(pp->LastPs&&pp->LastPs->p->Container && pp->LastPs->p->Container->Hyper==2 && pp->LastPs->UseInstance){
  2034. laMemNodeHyper* m=memGetHead(pp->LastPs->UseInstance,0); m->Modified=1; } }
  2035. else for(laPropStep* ps=pp->Go;ps;ps=ps->pNext){
  2036. if(ps->Type=='.' && ps->p->Container->Hyper==2 && ps->UseInstance){
  2037. laMemNodeHyper* m=memGetHead(ps->UseInstance,0); m->Modified=1; }
  2038. }
  2039. laMarkPropChanged(pp->RawThis);
  2040. }
  2041. void laMarkMemChanged(void* memuser){
  2042. int level; laMemNodeHyper* m=memGetHead(memuser,&level); if(level!=2) return;
  2043. m->Modified=1;
  2044. }
  2045. void laMarkMemClean(void* memuser){
  2046. int level; laMemNodeHyper* m=memGetHead(memuser,&level); if(level!=2) return;
  2047. m->Modified=0;
  2048. }
  2049. laPropContainer *la_SetGeneralRoot(laPropContainer **GeneralRoot, const char *Identifier, const char *Name, const char *Description){
  2050. laPropContainer* ret =memAcquire(sizeof(laPropContainer));
  2051. *GeneralRoot = ret;
  2052. ret->Identifier = Identifier;
  2053. ret->Name = Name;
  2054. ret->Description = Description;
  2055. ret->OtherAlloc=1;
  2056. lstAppendItem(&MAIN.PropContainers, ret);
  2057. return ret;
  2058. }
  2059. void laSetRootInstance(void *root){
  2060. MAIN.DataRoot.RootInstance = root;
  2061. }
  2062. laPropContainer *laDefineRoot(){
  2063. if (!MAIN.DataRoot.Root) MAIN.DataRoot.Root = laAddPropertyContainer("root", "__ROOT__", "Root Node In NUL4.0 Data System", U'🞈', 0, 0, 0, 0, 2|LA_PROP_OTHER_ALLOC);
  2064. return MAIN.DataRoot.Root;
  2065. }
  2066. void laThrowToTrashBin(void *Data, char *ContainerString){
  2067. laPropContainer *pc = la_ContainerLookup(ContainerString);
  2068. lstAppendItem(&pc->TrashBin, Data);
  2069. }
  2070. int laValidateHyper2(laPropContainer* pc, laPropContainer* ParentHyper2, laSafeString** errors){
  2071. int pass=1;
  2072. for(laProp* p=pc->Props.pFirst;p;p=p->Item.pNext){
  2073. if(p->PropertyType!=LA_PROP_SUB) continue;
  2074. laSubProp* sp=p;
  2075. if(!p->SubProp && !sp->GetType){
  2076. p->SubProp=la_ContainerLookup(sp->TargetID);
  2077. if(!p->SubProp){
  2078. strSafePrint(errors, "Unable to locate property container \"%s\".\n", sp->TargetID); pass=0; continue; }
  2079. laPropContainer* spc=p->SubProp;
  2080. laPropContainer* sNoHyper2 = ParentHyper2;
  2081. if(!ParentHyper2){
  2082. if(pc->Hyper==2 && (!(p->UDFNoCreate||p->UDFIsRefer||p->UDFIsSingle))){
  2083. sNoHyper2=pc; }
  2084. }
  2085. if(sNoHyper2){
  2086. if(spc->Hyper==2 && !sp->Base.UDFIsRefer){ strSafePrint(errors,\
  2087. "Hyper2 list (\"%s\") under another Hyper2 container (\"%s\") is not allowed.\n",
  2088. sp->TargetID, sNoHyper2->Identifier); pass=0; }
  2089. }
  2090. if(!laValidateHyper2(p->SubProp, sNoHyper2, errors)){pass=0;}
  2091. if(ParentHyper2) ParentHyper2->validated=1;
  2092. }
  2093. }
  2094. return pass;
  2095. }
  2096. int laValidateProperties(){
  2097. laSafeString* errors=0; int pass=1;
  2098. for(laPropContainer* pc=MAIN.PropContainers.pFirst;pc;pc=pc->Item.pNext){
  2099. if(pc->validated){continue;}
  2100. if(!laValidateHyper2(pc, 0, &errors)){ pass=0; }
  2101. pc->validated=1;
  2102. }
  2103. if(!pass){
  2104. printf("laValidateHyper2 Failed:\n"); printf("%s",SSTR(errors));
  2105. }
  2106. return pass;
  2107. }
  2108. //==================================[RW]
  2109. void la_GetWorkingDirectoryInternal(){
  2110. #ifdef LAGUI_ANDROID
  2111. strSafeSet(&MAIN.WorkingDirectory, ""); return;
  2112. #endif
  2113. char mbuf[2048];
  2114. #ifdef LA_LINUX
  2115. readlink("/proc/self/exe", mbuf, 2048);
  2116. strDiscardLastSegmentSeperateBy(mbuf, '/');
  2117. chdir(mbuf);
  2118. #endif
  2119. #ifdef _WIN32
  2120. GetModuleFileName(NULL, mbuf, 2048);
  2121. strDiscardLastSegmentSeperateBy(mbuf, '\\');
  2122. _chdir(mbuf);
  2123. #endif
  2124. int len=strlen(mbuf);if(mbuf[len]!=LA_PATH_SEP){ mbuf[len]=LA_PATH_SEP; mbuf[len+1]=0; }
  2125. strSafeSet(&MAIN.WorkingDirectory, mbuf);
  2126. }
  2127. int laGetRelativeDirectory(char *FullFrom, char *FullTo, char *Result){
  2128. return 0;
  2129. //WIN32_FIND_DATA fd;
  2130. //HANDLE h;
  2131. //char *PathFrom = 0, *PathTo = 0;
  2132. //char FullFromP[1024], FullToP[1024]={0};
  2133. //char *FileNameOnly = 0;
  2134. //int seg, i;
  2135. //
  2136. //Result[0] = 0;
  2137. //
  2138. //if (!FullFrom || !FullTo || FullFrom[0] != FullTo[0])
  2139. // return 0;
  2140. //
  2141. //strcpy(FullFromP, FullFrom);
  2142. //strcpy(FullToP, FullTo);
  2143. //
  2144. //while (h = FindFirstFile(FullFromP, &fd))
  2145. //{
  2146. // if (h == INVALID_HANDLE_VALUE || (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
  2147. // {
  2148. // strDiscardLastSegmentSeperateBy(FullFromP, '/');
  2149. // continue;
  2150. // }
  2151. // break;
  2152. //}
  2153. //
  2154. //while (h = FindFirstFile(FullToP, &fd))
  2155. //{
  2156. // if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  2157. // {
  2158. // FileNameOnly = strGetLastSegment(FullToP, '/');
  2159. // strDiscardLastSegmentSeperateBy(FullToP, '/');
  2160. // continue;
  2161. // }
  2162. // elif (h == INVALID_HANDLE_VALUE)
  2163. // {
  2164. // strDiscardLastSegmentSeperateBy(FullToP, '/');
  2165. // continue;
  2166. // }
  2167. // break;
  2168. //}
  2169. //
  2170. //strDiscardSameBeginningSeperatedBy(FullFromP, FullToP, &PathFrom, &PathTo, '/');
  2171. //
  2172. //seg = strCountSegmentSeperateBy(PathFrom, '/');
  2173. //
  2174. //for (i = 0; i < seg; i++)
  2175. //{
  2176. // strcat(Result, "../");
  2177. //}
  2178. //
  2179. //strcat(Result, PathTo);
  2180. //
  2181. //if (FileNameOnly)
  2182. //{
  2183. // strcat(Result, "/");
  2184. // strcat(Result, FileNameOnly);
  2185. //}
  2186. //
  2187. //return 1;
  2188. }
  2189. void laGetUDFRelativeDirectory(laUDF *From, laUDF *To, char *Result){
  2190. if (!From || !To) return;
  2191. laGetRelativeDirectory(From->FileName->Ptr, To->FileName->Ptr, Result);
  2192. }
  2193. void laGetFullPath(char *FullFrom, char *Relative, char *Result){
  2194. //WIN32_FIND_DATA fd;
  2195. //HANDLE h;
  2196. //char *PathFrom = 0, *PathTo = 0;
  2197. //char FullFromP[1024] = {0};
  2198. //int seg, i;
  2199. //
  2200. //Result[0] = 0;
  2201. //
  2202. //if (!FullFrom)
  2203. // return 0;
  2204. //
  2205. //strcpy(FullFromP, FullFrom);
  2206. //
  2207. //while (h = FindFirstFile(FullFromP, &fd))
  2208. //{
  2209. // if (h == INVALID_HANDLE_VALUE || (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
  2210. // {
  2211. // strDiscardLastSegmentSeperateBy(FullFromP, '/');
  2212. // continue;
  2213. // }
  2214. // break;
  2215. //}
  2216. //strcpy(Result, FullFromP);
  2217. ////strcat(Result, "/");
  2218. //strcat(Result, Relative);
  2219. }
  2220. laUDF *laPrepareUDF(char *FileName){
  2221. laUDF *udf = memAcquire(sizeof(laUDF));
  2222. strSafeSet(&udf->FileName, FileName);
  2223. return udf;
  2224. }
  2225. void laWriteProp(laUDF *udf, char *Path){
  2226. laUDFPropSegment *ps = CreateNew(laUDFPropSegment);
  2227. strSafeSet(&ps->Path, Path);
  2228. la_GetPropFromPath(&ps->PP, 0, Path, 0);
  2229. la_StepPropPack(&ps->PP);
  2230. lstAppendItem(&udf->PropsToOperate, ps);
  2231. udf->NumSegmets++;
  2232. }
  2233. void laWritePropP(laUDF *udf, laPropPack *pp){
  2234. char buf[LA_RAW_CSTR_MAX_LEN] = {0};
  2235. laUDFPropSegment *ps = CreateNew(laUDFPropSegment);
  2236. ps->PPP = pp;
  2237. udf->NumSegmets++;
  2238. //la_GetPropPackFullPath(pp, Buf);
  2239. //strSafeSet(&ps->Path, Buf);
  2240. lstAppendItem(&udf->PropsToOperate, ps);
  2241. }
  2242. void la_IncludeHyper2Instance(laUDF *udf, laPropContainer* pc, void* Instance){
  2243. laUDFH2Instance *h2 = memAcquire(sizeof(laUDFH2Instance));
  2244. h2->pc=pc; h2->Instance = Instance;
  2245. lstAppendItem(&udf->H2Instances, h2);
  2246. udf->HasInstances=1;
  2247. }
  2248. void la_ClearHyper2Instances(laUDF* udf){
  2249. laUDFH2Instance *h2;
  2250. while(h2=lstPopItem(&udf->H2Instances)){ memFree(h2); }
  2251. udf->HasInstances=0;
  2252. }
  2253. //void la_UDFAppendPointerRecord(void* UseInstance, laSubProp* p, void** PendingReference,void* ReadInstance) {
  2254. // //laUDFPointerRecord* upr = &MAIN.PendingPointers[MAIN.NextPendingPointer];
  2255. // //if (!ReadInstance) return;
  2256. // //upr->ReadInstance = ReadInstance;
  2257. // //upr->PendingReference = PendingReference;
  2258. // //upr->SubProp = p;
  2259. // //upr->UseInstance = UseInstance;
  2260. // //MAIN.NextPendingPointer++;
  2261. //}
  2262. //void la_UDFAppendNUIDRecord(void* UseInstance, laSubProp* p, void** PendingReference, char * ReadNUID) {
  2263. // //laUDFPointerRecord* upr = &MAIN.PendingPointers[MAIN.NextPendingPointer];
  2264. // //if (!ReadNUID) return;
  2265. // //upr->NUID = CreateNewBuffer(char, 23);
  2266. // //strcpy(upr->NUID, ReadNUID);
  2267. // //upr->PendingReference = PendingReference;
  2268. // //upr->SubProp = p;
  2269. // //upr->UseInstance = UseInstance;
  2270. // //MAIN.NextPendingPointer++;
  2271. //}
  2272. //void la_UDFAppendPointerSync(void* ReadInstance, void* ActualInstance) {
  2273. // //laUDFPointerSync* ups = CreateNew(laUDFPointerSync);
  2274. // //ups->ReadPointer = ReadInstance;
  2275. // //ups->ActualPointer = ActualInstance;
  2276. // //hsh65536InsertItem(&MAIN.PointerSync, ups, ReadInstance);
  2277. //}
  2278. //void la_UDFAppendNUIDSync(char * ReadNUID, void* ActualInstance) {
  2279. // //laUDFPointerSync* ups = CreateNew(laUDFPointerSync);
  2280. // //ups->NUID = CreateNewBuffer(char, 23);
  2281. // //memcpy(ups->NUID, ReadNUID,sizeof(char)*23);
  2282. // //ups->ActualPointer = ActualInstance;
  2283. // //hsh65536InsertItem(&MAIN.PointerSync, ups, (long)ReadNUID[15]);
  2284. //}
  2285. void la_UDFAppendSharedTypePointer(char *ID, void *Pointer){
  2286. laSharedTypeItem *sti = memAcquireSimple(sizeof(laSharedTypeItem));
  2287. strCopyFull(sti->ID, ID);
  2288. sti->Pointer = Pointer;
  2289. lstAppendItem(&MAIN.SharedTypePointerSync, sti);
  2290. }
  2291. #define la_UseUDF(udf) udf->Used = 1
  2292. u64bit la_Tell(laUDF *udf){
  2293. if (udf->DiskFile) return ftell(udf->DiskFile);
  2294. else return udf->Seek;
  2295. }
  2296. int la_Seek(laUDF *udf, u64bit Offset){
  2297. if (udf->DiskFile) return fseek(udf->DiskFile, Offset, SEEK_SET);
  2298. udf->Seek = Offset;
  2299. return 1;
  2300. }
  2301. u64bit la_FileSize(laUDF* udf){
  2302. if(udf->DiskFile){ u64bit a=ftell(udf->DiskFile); fseek(udf->DiskFile,0,SEEK_END); u64bit sz=ftell(udf->DiskFile); fseek(udf->DiskFile,a,SEEK_SET); return sz; }
  2303. return udf->FileContentSize;
  2304. }
  2305. void la_WriteString(laUDF *udf, const char *String){
  2306. short size = strlen(String);
  2307. fwrite(&size, sizeof(short), 1, udf->DiskFile);
  2308. if (size) fwrite(String, size*sizeof(char), 1, udf->DiskFile);
  2309. }
  2310. void la_WriteOnlyString(laUDF *udf, const char *String){
  2311. fwrite(String, strlen(String), 1, udf->DiskFile);
  2312. }
  2313. void la_WriteOnlyMBString(laUDF *udf, const char *String){
  2314. fwrite(String, strlen(String), 1, udf->DiskFile);
  2315. }
  2316. void la_WriteInt(laUDF *udf, int Data){
  2317. fwrite(&Data, sizeof(int), 1, udf->DiskFile);
  2318. }
  2319. void la_WriteUInt(laUDF *udf, uint32_t Data){
  2320. fwrite(&Data, sizeof(uint32_t), 1, udf->DiskFile);
  2321. }
  2322. void la_WriteUByte(laUDF *udf, unsigned char Data){
  2323. fwrite(&Data, sizeof(unsigned char), 1, udf->DiskFile);
  2324. }
  2325. void la_WriteShort(laUDF *udf, short Data){
  2326. fwrite(&Data, sizeof(short), 1, udf->DiskFile);
  2327. }
  2328. void la_WriteLong(laUDF *udf, u64bit Data){
  2329. fwrite(&Data, sizeof(u64bit), 1, udf->DiskFile);
  2330. }
  2331. void la_WritePointer(laUDF *udf, void *Data){
  2332. fwrite(&Data, sizeof(void *), 1, udf->DiskFile);
  2333. }
  2334. void la_WriteFloat(laUDF *udf, real Data){
  2335. fwrite(&Data, sizeof(real), 1, udf->DiskFile);
  2336. }
  2337. void la_WriteSized(laUDF *udf, void* Data, int size){
  2338. printf("%llx %d\n",Data,size);
  2339. fwrite(Data, size, 1, udf->DiskFile);
  2340. }
  2341. short la_ReadShort(laUDF *udf){
  2342. short result;
  2343. if(!udf->FileContent){ fread(&result, sizeof(short), 1, udf->DiskFile); }
  2344. else{ memcpy(&result, udf->FileContent + udf->Seek, sizeof(short)); udf->Seek += sizeof(short); }
  2345. return result;
  2346. }
  2347. u8bit la_ReadUByte(laUDF *udf){
  2348. u8bit result;
  2349. if(!udf->FileContent){ fread(&result, sizeof(u8bit), 1, udf->DiskFile); }
  2350. else{ memcpy(&result, udf->FileContent + udf->Seek, sizeof(u8bit)); udf->Seek += sizeof(u8bit); }
  2351. return result;
  2352. }
  2353. int la_ReadInt(laUDF *udf){
  2354. int result;
  2355. if(!udf->FileContent){ fread(&result, sizeof(int), 1, udf->DiskFile); }
  2356. else{ memcpy(&result, udf->FileContent + udf->Seek, sizeof(int)); udf->Seek += sizeof(int); }
  2357. return result;
  2358. }
  2359. uint32_t la_ReadUInt(laUDF *udf){
  2360. uint32_t result;
  2361. if(!udf->FileContent){ fread(&result, sizeof(uint32_t), 1, udf->DiskFile); }
  2362. else{ memcpy(&result, udf->FileContent + udf->Seek, sizeof(uint32_t)); udf->Seek += sizeof(uint32_t); }
  2363. return result;
  2364. }
  2365. u64bit la_ReadLong(laUDF *udf){
  2366. u64bit result;
  2367. if(!udf->FileContent){ fread(&result, sizeof(u64bit), 1, udf->DiskFile); }
  2368. else{ memcpy(&result, udf->FileContent + udf->Seek, sizeof(u64bit)); udf->Seek += sizeof(u64bit); }
  2369. return result;
  2370. }
  2371. void *la_ReadPointer(laUDF *udf){
  2372. u64bit result = 0;
  2373. if(!udf->FileContent){ fread(&result, sizeof(u64bit), 1, udf->DiskFile); }
  2374. else{ memcpy(&result, udf->FileContent + udf->Seek, sizeof(u64bit)); udf->Seek += sizeof(u64bit); }
  2375. return (void *)result;
  2376. }
  2377. real la_ReadFloat(laUDF *udf){
  2378. real result;
  2379. if(!udf->FileContent){ fread(&result, sizeof(real), 1, udf->DiskFile); }
  2380. else{ memcpy(&result, udf->FileContent + udf->Seek, sizeof(real)); udf->Seek += sizeof(real); }
  2381. return result;
  2382. }
  2383. void la_ReadString(laUDF *udf, char *buffer){
  2384. short len = la_ReadShort(udf);
  2385. if (len){
  2386. if(!udf->FileContent){ fread(buffer, sizeof(char) * len, 1, udf->DiskFile); }
  2387. else{ memcpy(buffer, udf->FileContent + udf->Seek, sizeof(char) * len); udf->Seek += sizeof(char) * len; }
  2388. }
  2389. buffer[len] = 0;
  2390. }
  2391. void la_ReadOnlyString(laUDF *udf, short len, char *buffer){
  2392. if(!udf->FileContent){ fread(buffer, sizeof(char) * len, 1, udf->DiskFile); }
  2393. else{ memcpy(buffer, udf->FileContent + udf->Seek, sizeof(char) * len); udf->Seek += sizeof(char) * len; }
  2394. buffer[len] = 0;
  2395. }
  2396. void la_ReadHyperData(laUDF *udf, void* HyperUserMem){
  2397. short NumUsers, i;
  2398. //void *CreatorInstance = la_ReadLong(udf);
  2399. laMemNodeHyper h_ = {0};
  2400. laMemNodeHyper *h=HyperUserMem?memGetHead(HyperUserMem, 0):&h_;
  2401. //la_UDFAppendPointerRecord(0, 0, &h->CreatedBy,CreatorInstance);
  2402. la_ReadString(udf, h->NUID.String);
  2403. h->TimeCreated.Year = la_ReadShort(udf);
  2404. h->TimeCreated.Month = la_ReadUByte(udf);
  2405. h->TimeCreated.Day = la_ReadUByte(udf);
  2406. h->TimeCreated.Hour = la_ReadUByte(udf);
  2407. h->TimeCreated.Minute = la_ReadUByte(udf);
  2408. h->TimeCreated.Second = la_ReadUByte(udf);
  2409. }
  2410. void la_PeekHyperUID(laUDF *udf, char* buf){
  2411. int pos=la_Tell(udf);
  2412. la_ReadString(udf, buf);
  2413. la_Seek(udf, pos);
  2414. }
  2415. void la_ReadBuffer(laUDF *udf, u64bit Size, void *Result){
  2416. if(!udf->FileContent){ fread(Result, Size, 1, udf->DiskFile); }
  2417. else{ memcpy(Result, udf->FileContent + udf->Seek, Size); udf->Seek += Size; }
  2418. }
  2419. void* la_ReadRaw(laUDF *udf, uint32_t* _sz){
  2420. uint32_t _size = la_ReadUInt(udf);
  2421. if(_sz){
  2422. if (_size){
  2423. void* data=calloc(1,_size);
  2424. la_ReadBuffer(udf, _size, data);
  2425. if(_sz) *_sz=_size;
  2426. return data;
  2427. } return 0;
  2428. }else{
  2429. la_Seek(udf, la_Tell(udf)+_size); return 0;
  2430. }
  2431. }
  2432. void la_WriteSingleProperty(laUDF *udf, void *FromInstance, laProp *p){
  2433. la_WriteString(udf, p->Identifier);
  2434. }
  2435. void la_GetPropPackFullPath(laPropPack *pp, char *result){
  2436. char buf[1024], upbuf[1024]={0}; if(!pp){result[0]=0; return;}
  2437. laPropStep *ps;
  2438. buf[0] = U'\0';
  2439. upbuf[0] = 0;
  2440. if (pp->Go){
  2441. for (ps = pp->Go; ps; ps = ps->pNext){
  2442. //if (!ps->p||!ps->p->Identifier) strcat(buf, "?");
  2443. if(ps->Type=='.') strcat(buf, ps->p->Identifier);
  2444. if(ps->Type=='#' ||ps->Type=='@' ||ps->Type=='=') {char b2[2]={0}; b2[0]= ps->Type; strcat(buf, b2); strcat(buf, ps->p);}
  2445. if (ps->pNext && ps->pNext->Type=='.') strcat(buf, ".");
  2446. }
  2447. }else{
  2448. if(pp->LastPs){ if(pp->LastPs->p->Identifier) strcat(buf, pp->LastPs->p->Identifier); else { strcat(buf,"?"); } }
  2449. if (pp->RawThis && pp->RawThis->LastPs->p != pp->LastPs->p) strcat(buf, pp->LastPs->p->Identifier);
  2450. }
  2451. if (pp->RawThis){
  2452. la_GetPropPackFullPath(pp->RawThis, upbuf);
  2453. if (pp->RawThis->LastPs->p != pp->LastPs->p) strcat(upbuf, ".");
  2454. strcat(upbuf, buf);
  2455. strCopyFull(result, upbuf);
  2456. }else
  2457. strCopyFull(result, buf);
  2458. }
  2459. void la_GetPropPackPath(laPropPack *pp, char *result){
  2460. char buf[1024], upbuf[1024]={0};
  2461. laPropStep *ps;
  2462. buf[0] = U'\0';
  2463. upbuf[0] = 0;
  2464. char Sep[2] = {0};
  2465. for (ps = pp->Go; ps; ps = ps->pNext){
  2466. if (!ps->p->Identifier) break;
  2467. if (ps->Type == U'.' || !ps->Type) strcat(buf, ps->p->Identifier);
  2468. else
  2469. strcat(buf, ps->p);
  2470. if (ps->pNext) strcat(buf, (Sep[0] = ((laPropStep *)ps->pNext)->Type) ? Sep : ".");
  2471. }
  2472. strCopyFull(result, buf);
  2473. }
  2474. int la_ReadIntProp(laUDF *udf, laPropPack *pp){
  2475. laProp *p;
  2476. int Data[16] = {0};
  2477. int len = 0;
  2478. int i;
  2479. if (!pp){
  2480. int mode = la_ReadShort(udf);
  2481. if (mode == LA_UDF_ARRAY_MARK_32){
  2482. len = la_ReadShort(udf);
  2483. for (i = 0; i < len; i++)
  2484. la_ReadInt(udf);
  2485. }else
  2486. Data[0] = la_ReadInt(udf);
  2487. return Data[0];
  2488. }
  2489. p = pp->LastPs->p;
  2490. short mark=la_ReadShort(udf);
  2491. if(mark==LA_UDF_ARRAY_MARK_64 || mark==LA_UDF_ARRAY_MARK_32){ len = la_ReadShort(udf);
  2492. for(i=0;i<len;i++){ Data[i] = la_ReadInt(udf); }
  2493. }else{
  2494. Data[0] = la_ReadInt(udf);
  2495. }
  2496. if (p->PropertyType & LA_PROP_ARRAY){
  2497. if (pp) laReadIntArrayAllArray(pp, Data);
  2498. }else{
  2499. if (pp) laReadInt(pp, Data[0]);
  2500. }
  2501. return Data[0];
  2502. }
  2503. real la_ReadFloatProp(laUDF *udf, laPropPack *pp){
  2504. laProp *p;
  2505. real Data[16] = {0};
  2506. int len = 0; int i;
  2507. if (!pp){
  2508. int mode = la_ReadShort(udf);
  2509. if (mode == LA_UDF_ARRAY_MARK_64){
  2510. len = la_ReadShort(udf);
  2511. for (i = 0; i < len; i++)
  2512. la_ReadFloat(udf);
  2513. }else
  2514. Data[0] = la_ReadFloat(udf);
  2515. return Data[0];
  2516. }
  2517. p = pp->LastPs->p;
  2518. short mark=la_ReadShort(udf);
  2519. if(mark==LA_UDF_ARRAY_MARK_64 || mark==LA_UDF_ARRAY_MARK_32){ len = la_ReadShort(udf);
  2520. for(i=0;i<len;i++){ Data[i] = la_ReadFloat(udf); }
  2521. }else{
  2522. Data[0] = la_ReadFloat(udf);
  2523. }
  2524. if (p->PropertyType & LA_PROP_ARRAY){
  2525. if (pp) laReadFloatArrayAllArray(pp, Data);
  2526. }else{
  2527. if (pp) laReadFloat(pp, Data[0]);
  2528. }
  2529. return Data[0];
  2530. }
  2531. void la_ReadStringProp(laUDF *udf, laPropPack *pp){
  2532. char buf[LA_RAW_CSTR_MAX_LEN]={0}; //XXX: long string not correct...
  2533. int len = 0;
  2534. int i;
  2535. buf[0] = 0;
  2536. la_ReadShort(udf);
  2537. la_ReadString(udf, buf);
  2538. if (pp) laReadString(pp, buf);
  2539. }
  2540. void la_ReadStringPropAsIdentifier(laUDF *udf, char *buf){
  2541. //char buf[LA_RAW_CSTR_MAX_LEN]={0};
  2542. int len = 0;
  2543. int i;
  2544. buf[0] = 0;
  2545. la_ReadShort(udf);
  2546. la_ReadString(udf, buf);
  2547. }
  2548. void la_WriteIntProp(laUDF *udf, laPropPack *pp){
  2549. laProp *p = pp->LastPs->p;
  2550. int Data[16] = {0};
  2551. int len = 0;
  2552. int i;
  2553. if (p->PropertyType & LA_PROP_ARRAY){
  2554. la_WriteShort(udf, LA_UDF_ARRAY_MARK_32);
  2555. laGetIntArray(pp, Data);
  2556. len = laGetArrayLength(pp);
  2557. la_WriteShort(udf, len);
  2558. for (i = 0; i < len; i++){
  2559. la_WriteInt(udf, Data[i]);
  2560. }
  2561. }else{
  2562. la_WriteShort(udf, LA_UDF_REGULAR_MARK_32);
  2563. Data[0] = laGetInt(pp);
  2564. la_WriteInt(udf, Data[0]);
  2565. }
  2566. }
  2567. void la_WriteFloatProp(laUDF *udf, laPropPack *pp){
  2568. laProp *p = pp->LastPs->p;
  2569. real Data[16] = {0};
  2570. int len = 0;
  2571. int i;
  2572. if (p->PropertyType & LA_PROP_ARRAY){
  2573. la_WriteShort(udf, LA_UDF_ARRAY_MARK_64);
  2574. laGetFloatArray(pp, Data);
  2575. len = laGetArrayLength(pp);
  2576. la_WriteShort(udf, len);
  2577. for (i = 0; i < len; i++){
  2578. la_WriteFloat(udf, Data[i]);
  2579. }
  2580. }else{
  2581. la_WriteShort(udf, LA_UDF_REGULAR_MARK_64);
  2582. Data[0] = laGetFloat(pp);
  2583. la_WriteFloat(udf, Data[0]);
  2584. }
  2585. }
  2586. void la_WriteStringProp(laUDF *udf, laPropPack *pp){
  2587. laProp *p = pp->LastPs->p;
  2588. char _buf[LA_RAW_CSTR_MAX_LEN]={0}; char* buf=_buf;
  2589. int len = 0;
  2590. int i;
  2591. buf[0] = 0;
  2592. la_WriteShort(udf, LA_UDF_STRING_MARK);
  2593. laGetString(pp, _buf, &buf);
  2594. la_WriteString(udf, buf);
  2595. }
  2596. void la_ReadEnumProp(laUDF *udf, laPropPack *pp, char* orig_string){
  2597. laProp *p;
  2598. int Data[16] = {0};
  2599. int len = 0;
  2600. int i;
  2601. char buf[LA_RAW_CSTR_MAX_LEN]={0};
  2602. if (!pp){
  2603. int mode = la_ReadShort(udf);
  2604. if (mode == LA_UDF_ARRAY_MARK_32){
  2605. len = la_ReadShort(udf);
  2606. for (i = 0; i < len; i++)
  2607. la_ReadString(udf, buf); //la_ReadInt(udf);
  2608. }else
  2609. la_ReadString(udf, buf); //la_ReadInt(udf);
  2610. if(orig_string)strcpy(orig_string, buf);
  2611. return;
  2612. }
  2613. p = pp->LastPs->p;
  2614. if (p->PropertyType & LA_PROP_ARRAY){
  2615. la_ReadShort(udf);
  2616. len = la_ReadShort(udf);
  2617. for (i = 0; i < len; i++){
  2618. la_ReadString(udf, buf);
  2619. Data[i] = laGetEnumFromIdentifier(p, buf)->Index;
  2620. //Data[i] = la_ReadInt(udf);
  2621. }
  2622. if (pp) laReadEnumArrayAll(pp, Data);
  2623. }else{
  2624. la_ReadShort(udf);
  2625. la_ReadString(udf, buf);
  2626. Data[0] = laGetEnumFromIdentifier(p, buf)->Index;
  2627. //Data[0] = la_ReadInt(udf);
  2628. if (pp) laReadEnum(pp, Data[0]);
  2629. }
  2630. }
  2631. void la_WriteEnumProp(laUDF *udf, laPropPack *pp){
  2632. laProp *p = pp->LastPs->p;
  2633. laEnumItem *Data[16] = {0};
  2634. int len = 0;
  2635. int i;
  2636. if (p->PropertyType & LA_PROP_ARRAY){
  2637. la_WriteShort(udf, (LA_UDF_ARRAY_MARK_32 | LA_UDF_STRING_MARK));
  2638. laGetEnumArray(pp, Data);
  2639. len = laGetArrayLength(pp);
  2640. la_WriteShort(udf, len);
  2641. for (i = 0; i < len; i++){
  2642. la_WriteString(udf, Data[i]->Identifier);
  2643. //la_WriteInt(udf, Data[i]->Index);
  2644. }
  2645. }else{
  2646. la_WriteShort(udf, LA_UDF_STRING_MARK);
  2647. Data[0] = laGetEnum(pp);
  2648. la_WriteString(udf, Data[0]->Identifier);
  2649. //la_WriteInt(udf, Data[0]->Index);
  2650. }
  2651. }
  2652. void la_ReadRawProp(laUDF *udf, laPropPack *pp){
  2653. la_ReadShort(udf);//mark
  2654. if (pp) {
  2655. uint32_t _size=0;
  2656. void* data=la_ReadRaw(udf,&_size);
  2657. laSetRaw(pp, data, _size); free(data);
  2658. }else{
  2659. la_ReadRaw(udf,0);
  2660. }
  2661. }
  2662. void la_WriteRawProp(laUDF *udf, laPropPack *pp){
  2663. laProp *p = pp->LastPs->p;
  2664. la_WriteShort(udf, LA_UDF_RAW_MARK);
  2665. if(laCanGetMultiRaw(p,pp->LastPs->UseInstance)){
  2666. int chunks=0; uint32_t sizes[128]={0}; void* pointers[128]={0};
  2667. uint32_t totalsize=0;
  2668. if(laGetMultiRaw(pp,&chunks,sizes,pointers)){ TNS_CLAMP(chunks,1,128);
  2669. for(int i=0;i<chunks;i++){ totalsize+=sizes[i]; } la_WriteUInt(udf,totalsize);
  2670. if(totalsize){
  2671. for(int i=0;i<chunks;i++){ if(sizes[i] && pointers[i]){ la_WriteSized(udf,pointers[i],sizes[i]); free(pointers[i]); } }
  2672. }
  2673. }else{
  2674. la_WriteUInt(udf,0); // no size
  2675. }
  2676. return;
  2677. }
  2678. void* data; uint32_t _size=0, IsCopy=0;
  2679. data=laGetRaw(pp, &_size, &IsCopy);
  2680. if(!data){ _size=0; }
  2681. la_WriteUInt(udf,_size);
  2682. if(_size){ la_WriteSized(udf,data,_size); }
  2683. if(IsCopy && data){ free(data); }
  2684. }
  2685. void la_WriteHyperData(laUDF *udf, void *HyperUserMem){
  2686. int loc = 0, res = 0, i = 0;
  2687. laItemUserLinker *iul;
  2688. laMemNodeHyper* h=memGetHead(HyperUserMem, 0);
  2689. //la_WriteLong(udf, h->CreatedBy);
  2690. la_WriteString(udf, h->NUID.String);
  2691. la_WriteShort(udf, h->TimeCreated.Year);
  2692. la_WriteUByte(udf, h->TimeCreated.Month);
  2693. la_WriteUByte(udf, h->TimeCreated.Day);
  2694. la_WriteUByte(udf, h->TimeCreated.Hour);
  2695. la_WriteUByte(udf, h->TimeCreated.Minute);
  2696. la_WriteUByte(udf, h->TimeCreated.Second);
  2697. }
  2698. laSharedTypeItem *la_ReferringToSharedResource(void *p){
  2699. laSharedTypeItem *sti;
  2700. for (sti = MAIN.SharedTypePointerSync.pFirst; sti; sti = sti->Item.pNext){
  2701. if (sti->Pointer == p)
  2702. return sti;
  2703. }
  2704. return 0;
  2705. }
  2706. void *la_FindSharedResouce(char *id){
  2707. laSharedTypeItem *sti;
  2708. for (sti = MAIN.SharedTypePointerSync.pFirst; sti; sti = sti->Item.pNext){
  2709. if (strSame(id, sti->ID))
  2710. return sti->Pointer;
  2711. }
  2712. return 0;
  2713. }
  2714. void la_AppendHyperRecord(laUDF *udf, void *HyperUserMem, laPropContainer* pc, u64bit Seek){
  2715. // this happens when theres a type{base{} ... } like tnsobject, should not be problematic...?
  2716. if(lstHasPointer(&udf->HyperRecords, HyperUserMem)) return;
  2717. laUDFHyperRecordItem *hri = lstAppendPointerSized(&udf->HyperRecords, HyperUserMem, sizeof(laUDFHyperRecordItem));
  2718. hri->Seek = Seek;
  2719. hri->pc = pc;
  2720. }
  2721. void la_NextH2Instance(laUDF* udf){
  2722. udf->CurrentH2Instance=(udf->CurrentH2Instance?udf->CurrentH2Instance->Item.pNext:0);
  2723. }
  2724. int la_WriteProp(laUDF *udf, laPropPack *pp, int FromThis, int UseInstanceList){
  2725. laProp *p = pp->LastPs->p, *subp = 0;
  2726. laPropStep SubPS = {0};
  2727. laPropPack SubPP = {0};
  2728. laPropIterator pi = {0};
  2729. laSharedTypeItem *sti;
  2730. laMemNodeHyper *hi;
  2731. void *inst = 0;
  2732. u64bit pReserve = 0, pContinue = 0;
  2733. int ItemNum = 0, PropNum = 0;
  2734. int counted = 0;
  2735. u64bit pEachCount;
  2736. SubPP.LastPs = &SubPS;
  2737. if (p->PropertyType == LA_PROP_OPERATOR) return 0;
  2738. if ((!pp->RawThis) || FromThis){
  2739. char FullPath[1024]={0};
  2740. FullPath[0] = 0;
  2741. la_GetPropPackFullPath(pp, FullPath);
  2742. la_WriteString(udf, FullPath);
  2743. }else{
  2744. la_WriteString(udf, p->Identifier);
  2745. }
  2746. switch (p->PropertyType){
  2747. case LA_PROP_SUB:
  2748. if (p->UDFIsRefer){
  2749. inst = laGetActiveInstanceStrict(p, pp->LastPs->UseInstance);
  2750. if (sti = la_ReferringToSharedResource(inst)){
  2751. la_WriteShort(udf, LA_UDF_SHARE_RESOURCE);
  2752. la_WriteString(udf, sti->ID);
  2753. }else{
  2754. if (!p->SubProp){
  2755. p->SubProp = la_ContainerLookup(((laSubProp *)p)->TargetID);
  2756. }
  2757. if (p->SubProp->Hyper == 2){
  2758. la_WriteShort(udf, LA_UDF_REFER | LA_UDF_HYPER_ITEM);
  2759. if (!inst) la_WriteString(udf, "");
  2760. else
  2761. la_WriteString(udf, ((laMemNodeHyper *)memGetHead(inst,0))->NUID.String);
  2762. udf->TotalRefs++;
  2763. }else{
  2764. la_WriteShort(udf, LA_UDF_REFER);
  2765. la_WritePointer(udf, inst);
  2766. }
  2767. pp->EndInstance = inst;
  2768. }
  2769. udf->TotalRefs++;
  2770. }else{
  2771. la_EnsureSubTarget(p,0);
  2772. la_WriteShort(udf, LA_UDF_COLLECTION);
  2773. pReserve = la_Tell(udf); la_WriteInt(udf, 0); //num items
  2774. if (((laSubProp*)p)->GetType) la_WriteInt(udf, LA_UDF_VARIABLE_NODE_SIZE);
  2775. else la_WriteInt(udf, LA_UDF_FIXED_NODE_SIZE);
  2776. la_WriteShort(udf, LA_UDF_ACTIVE);
  2777. la_WritePointer(udf, laGetActiveInstance(p, pp->LastPs->UseInstance, &pi));
  2778. if (FromThis){
  2779. inst = pp->EndInstance;
  2780. }else{
  2781. inst = laGetInstance(p, pp->LastPs->UseInstance, &pi);
  2782. pp->EndInstance = inst;
  2783. }
  2784. while (inst){
  2785. if(((laSubProp*)p)->SaveFilter && (!((laSubProp*)p)->SaveFilter(pp->LastPs->UseInstance, inst))){
  2786. inst = laGetNextInstance(p, inst, &pi); pp->EndInstance = inst; continue;
  2787. }
  2788. laPropContainer* pc=p->SubProp; int need_type=0;
  2789. if (((laSubProp*)p)->GetType){ pc=((laSubProp*)p)->GetType(inst); need_type=1; }
  2790. if (pc->Hyper == 2){
  2791. void* compare=udf->CurrentH2Instance?udf->CurrentH2Instance->Instance:0;
  2792. if((!p->UDFIsSingle) && UseInstanceList && inst!=compare){
  2793. inst = laGetNextInstance(p, inst, &pi); pp->EndInstance = inst; continue;}
  2794. if(need_type){ la_WriteString(udf, pc->Identifier); }
  2795. hi = inst;
  2796. la_WriteShort(udf, LA_UDF_HYPER_ITEM);
  2797. la_AppendHyperRecord(udf, inst, p->SubProp, la_Tell(udf));
  2798. la_WriteHyperData(udf, inst);
  2799. memMarkClean(inst);
  2800. }else{
  2801. if(need_type){ la_WriteString(udf, pc->Identifier); }
  2802. la_WriteShort(udf, LA_UDF_ITEM);
  2803. }
  2804. ItemNum++;
  2805. la_WritePointer(udf, inst);
  2806. la_WriteInt(udf, laGetUiState(p, pp->LastPs->UseInstance));
  2807. PropNum=0; pEachCount = la_Tell(udf); la_WriteShort(udf, 0);
  2808. for (subp = pc->Props.pFirst; subp; subp = subp->Item.pNext){
  2809. if (subp->UDFIgnore) continue;
  2810. SubPP.RawThis = pp;
  2811. SubPS.p = subp;
  2812. SubPS.UseInstance = inst;
  2813. if (la_WriteProp(udf, &SubPP, 0, UseInstanceList)) PropNum++;
  2814. }
  2815. if (FromThis){ inst = 0; }else{
  2816. inst = laGetNextInstance(p, inst, &pi);
  2817. pp->EndInstance = inst;
  2818. if(UseInstanceList&&pc->Hyper==2&&(!p->UDFIsSingle)&&(!p->UDFNoCreate)){
  2819. la_NextH2Instance(udf); if(!udf->CurrentH2Instance) inst=0; }
  2820. }
  2821. pContinue = la_Tell(udf);
  2822. la_Seek(udf, pEachCount); la_WriteShort(udf, PropNum);
  2823. la_Seek(udf, pContinue);
  2824. }
  2825. pContinue = la_Tell(udf);
  2826. la_Seek(udf, pReserve); la_WriteInt(udf, ItemNum);
  2827. la_Seek(udf, pContinue);
  2828. }
  2829. break;
  2830. case LA_PROP_INT:
  2831. case LA_PROP_ARRAY | LA_PROP_INT:
  2832. la_WriteIntProp(udf, pp);
  2833. break;
  2834. case LA_PROP_FLOAT:
  2835. case LA_PROP_ARRAY | LA_PROP_FLOAT:
  2836. la_WriteFloatProp(udf, pp);
  2837. break;
  2838. case LA_PROP_STRING:
  2839. la_WriteStringProp(udf, pp);
  2840. break;
  2841. case LA_PROP_ENUM:
  2842. case LA_PROP_ARRAY | LA_PROP_ENUM:
  2843. la_WriteEnumProp(udf, pp);
  2844. break;
  2845. case LA_PROP_RAW:
  2846. la_WriteRawProp(udf, pp);
  2847. break;
  2848. default:
  2849. break;
  2850. }
  2851. return 1;
  2852. }
  2853. void la_AddPostReadNode(void *Instance, laContainerPostReadFunc Func){
  2854. laUDFPostRead *upr = lstAppendPointerSized(&MAIN.PostReadNodes, Instance, sizeof(laUDFPostRead));
  2855. upr->Instance = Instance;
  2856. upr->Func = Func;
  2857. }
  2858. void *la_GetReadDBInstNUID(char *ReferReadNUID){
  2859. if (!ReferReadNUID) return 0;
  2860. laListHandle *l = hsh65536DoHashNUID(&MAIN.DBInst2, ReferReadNUID);
  2861. for (laMemNodeHyper* m = l->pFirst; m; m = m->Item.pNext){
  2862. if (!strcmp(ReferReadNUID, m->NUID.String))
  2863. return ((unsigned char*)m)+sizeof(laMemNodeHyper);
  2864. }
  2865. return 0;
  2866. }
  2867. void *la_GetReadDBInstPtr(void *ReferRead){
  2868. if (!ReferRead) return 0;
  2869. laListHandle *l = hsh65536DoHashLongPtr(&MAIN.DBInst1, ReferRead);
  2870. for (laMemNode* m = l->pFirst; m; m = m->Item.pNext){
  2871. if (ReferRead == m->ReadInstance)
  2872. return ((unsigned char*)m)+sizeof(laMemNode);
  2873. }
  2874. return 0;
  2875. }
  2876. void la_AddDataInst(void *ReadInstance, char *ReadNUID, void *ActualInstance){
  2877. laListHandle* l=0;
  2878. void* head=memGetHead(ActualInstance, 0);
  2879. if (ReadNUID) l = hsh65536DoHashNUID(&MAIN.DBInst2, ReadNUID);
  2880. else { l = hsh65536DoHashLongPtr(&MAIN.DBInst1, ReadInstance); ((laMemNode*)head)->ReadInstance = ReadInstance; }
  2881. lstPushItem(l, head); //always push so we get the latest during ptr sync.
  2882. }
  2883. laPtrSync *la_AddPtrSyncDirect(void *Refer, void *Parent, laProp *Sub){
  2884. laPtrSync *ps = memAcquireSimple(sizeof(laPtrSync));
  2885. ps->RefDB = Refer;
  2886. ps->Parent = Parent;
  2887. ps->Prop = Sub;
  2888. return ps;
  2889. }
  2890. laPtrSyncCommand *la_AddPtrSyncCommand(void *ReadRefer, void *ParentInst, char *ReadNUID, laProp *Sub){
  2891. laPtrSyncCommand *psc = memAcquireSimple(sizeof(laPtrSyncCommand));
  2892. psc->Target = la_AddPtrSyncDirect(0, ParentInst, Sub);
  2893. psc->ReadInstance = ReadRefer;
  2894. if (ReadNUID){ strcpy(psc->ReadNUID.String, ReadNUID); lstAppendItem(&MAIN.PtrSyncHyper2Commands, psc); }
  2895. else{ lstAppendItem(&MAIN.PtrSyncAddressCommands, psc); }
  2896. return psc;
  2897. }
  2898. void la_ResetInstance(void* inst, laPropContainer* pc, int IsListItem);
  2899. void laRequestAdditionalRegistry(laUDFRegistry* r);
  2900. laUDFOwnHyperItem* laNewHyperResource(char* uid);
  2901. laUDFOwnHyperItem* laFindHyperResource(char* uid);
  2902. void la_LoadAdditionalRegistries(){
  2903. laUDFRegistry* r;
  2904. while(r=lstPopPointer(&MAIN.PendingResourceRequests)){
  2905. laManagedUDF* m;
  2906. logPrint("[INFO] Loading additional resource: %s\n",r->Path->Ptr);
  2907. laUDF* udf = laOpenUDF(r->Path->Ptr, 1, 0, &m);
  2908. if (udf){ laExtractUDF(udf, m, LA_UDF_MODE_OVERWRITE); laCloseUDF(udf); }else{ logPrint("[WARN] Can't open resource: %s\n",r->Path->Ptr); }
  2909. }
  2910. }
  2911. void la_ExecutePtrSyncCommand(int Mode){
  2912. int i;
  2913. laPtrSyncCommand *psc,*NextPSC;
  2914. laPtrSync *ps;
  2915. void *dbi;
  2916. laListHandle *lps, *ldbi;
  2917. laListHandle L2 = {0};
  2918. int FailCount = 0, AllCount = 0;
  2919. while (psc = lstPopItem(&MAIN.PtrSyncAddressCommands)){
  2920. ps = psc->Target; dbi=0; if (psc->ReadInstance) dbi = la_GetReadDBInstPtr(psc->ReadInstance);
  2921. if (dbi){
  2922. //if (Mode == LA_udf_read)dbi->ReadInst = dbi->ActualInst; //??
  2923. ps->RefDB = dbi; laSetActiveInstance(ps->Prop, ps->Parent, dbi);
  2924. }else{ FailCount++; }
  2925. AllCount++; memFree(psc);
  2926. }
  2927. laUDFOwnHyperItem* ohi;
  2928. for(psc=MAIN.PtrSyncHyper2Commands.pFirst;psc;psc=NextPSC){
  2929. NextPSC=psc->Item.pNext; ps = psc->Target; dbi=0; if (psc->ReadNUID.String[0]) dbi = la_GetReadDBInstNUID(psc->ReadNUID.String);
  2930. if (dbi){
  2931. //if (Mode == LA_udf_read)dbi->ReadInst = dbi->ActualInst; //??
  2932. ps->RefDB = dbi; laSetActiveInstance(ps->Prop, ps->Parent, dbi);
  2933. lstRemoveItem(&MAIN.PtrSyncHyper2Commands,psc);memFree(psc);
  2934. }else{
  2935. if(!(ohi=laFindHyperResource(psc->ReadNUID.String))){
  2936. logPrint("Can't find resource %s\n", psc->ReadNUID.String);
  2937. lstRemoveItem(&MAIN.PtrSyncHyper2Commands,psc);memFree(psc);
  2938. FailCount++;
  2939. }else{
  2940. laRequestAdditionalRegistry(ohi->Registry);
  2941. lstRemoveItem(&MAIN.PtrSyncHyper2Commands,psc);
  2942. lstPushItem(&MAIN.PtrSyncHyper2Commands,psc); continue;
  2943. }
  2944. }
  2945. AllCount++;
  2946. }
  2947. logPrint("Reference Match: Total %d, Failed %d\n", AllCount, FailCount);
  2948. la_LoadAdditionalRegistries();
  2949. }
  2950. int la_ExtractFakeProp(laUDF *udf){
  2951. char buf[LA_RAW_CSTR_MAX_LEN] = {0};
  2952. void *Instance = 0;
  2953. void *ReadInstance;
  2954. int ReadState;
  2955. int NumItems;
  2956. short PropNumPerItem;
  2957. void *ActiveInstance;
  2958. int i, j;
  2959. int ItemType;
  2960. short mode, len;
  2961. int VariableNodeSize;
  2962. short PreMode;
  2963. mode = la_ReadShort(udf);
  2964. switch (mode){
  2965. case LA_UDF_REGULAR_MARK_32:
  2966. la_ReadInt(udf);
  2967. break;
  2968. case LA_UDF_REGULAR_MARK_64:
  2969. la_ReadFloat(udf);
  2970. break;
  2971. case (LA_UDF_ARRAY_MARK_32 | LA_UDF_STRING_MARK):
  2972. len = la_ReadShort(udf);
  2973. for (i = 0; i < len; i++)
  2974. la_ReadString(udf, buf);
  2975. break;
  2976. case LA_UDF_ARRAY_MARK_32:
  2977. len = la_ReadShort(udf);
  2978. for (i = 0; i < len; i++)
  2979. la_ReadInt(udf);
  2980. break;
  2981. case LA_UDF_ARRAY_MARK_64:
  2982. len = la_ReadShort(udf);
  2983. for (i = 0; i < len; i++)
  2984. la_ReadFloat(udf);
  2985. break;
  2986. case LA_UDF_STRING_MARK:
  2987. la_ReadString(udf, buf);
  2988. break;
  2989. case LA_UDF_SHARE_RESOURCE:
  2990. la_ReadString(udf, buf);
  2991. break;
  2992. case LA_UDF_REFER:
  2993. la_ReadPointer(udf);
  2994. break;
  2995. case (short)(LA_UDF_REFER | LA_UDF_HYPER_ITEM):
  2996. la_ReadString(udf, buf);
  2997. break;
  2998. case LA_UDF_COLLECTION:
  2999. NumItems = la_ReadInt(udf);
  3000. VariableNodeSize = la_ReadInt(udf);
  3001. VariableNodeSize = (VariableNodeSize == LA_UDF_VARIABLE_NODE_SIZE) ? 1 : 0;
  3002. la_ReadShort(udf); //active mark
  3003. ActiveInstance = la_ReadPointer(udf);
  3004. for (i = 0; i < NumItems; i++){
  3005. if(VariableNodeSize){ la_ReadString(udf,buf); }
  3006. ItemType = la_ReadShort(udf);
  3007. if (ItemType == LA_UDF_HYPER_ITEM){ la_ReadHyperData(udf, 0); }
  3008. ReadInstance = la_ReadPointer(udf);
  3009. ReadState = la_ReadInt(udf);
  3010. PropNumPerItem=la_ReadShort(udf);
  3011. for (j = 0; j < PropNumPerItem; j++){
  3012. la_ReadString(udf, buf);
  3013. la_ExtractFakeProp(udf);
  3014. }
  3015. }
  3016. break;
  3017. case LA_PROP_RAW:
  3018. la_ReadRawProp(udf, 0);
  3019. break;
  3020. default:
  3021. ReadState = ReadState;
  3022. break;
  3023. }
  3024. return 0;
  3025. }
  3026. int la_ExtractProp(laUDF *udf, laManagedUDF* mUDF, laPropPack *pp, void *ParentInst, int Mode){
  3027. laPropStep SubPS = {0};
  3028. laPropPack SubPP = {0};
  3029. char buf[LA_RAW_CSTR_MAX_LEN] = {0};
  3030. laProp *p = pp->LastPs->p, *subp;
  3031. short SubMode, ItemType = 0;
  3032. void *Instance = 0;
  3033. void *ReadInstance;
  3034. int ReadState;
  3035. int NumItems;
  3036. short PropNumPerItem;
  3037. void *ActiveInstance;
  3038. short PreReadMode;
  3039. int i, j;
  3040. int VariableNodeSize;
  3041. char NodeType[128]={0};
  3042. int EStatus = 0;
  3043. real ReadF;
  3044. int ReadI;
  3045. int IsExceptionNode = 0;
  3046. SubPP.LastPs = &SubPS;
  3047. switch (p->PropertyType){
  3048. case LA_PROP_SUB:
  3049. la_EnsureSubTarget(p, 0);
  3050. SubMode = la_ReadShort(udf);
  3051. if (SubMode == LA_UDF_SHARE_RESOURCE){
  3052. la_ReadString(udf, buf);
  3053. laSetActiveInstance(p, pp->LastPs->UseInstance, la_FindSharedResouce(buf));
  3054. }elif (SubMode == LA_UDF_REFER){
  3055. Instance = la_ReadPointer(udf);
  3056. if (Instance && p->Offset>=0){
  3057. if (p->OffsetIsPointer) la_AddPtrSyncCommand(Instance, ParentInst, 0, p);
  3058. else la_AddDataInst(Instance, 0, ((BYTE *)pp->LastPs->UseInstance + p->Offset));
  3059. }
  3060. }elif (SubMode == (short)(LA_UDF_REFER | LA_UDF_HYPER_ITEM)){
  3061. char NUID[32]={0};
  3062. NUID[0] = 0;
  3063. la_ReadString(udf, NUID);
  3064. if (NUID[0]){
  3065. la_AddPtrSyncCommand(0, ParentInst, NUID, p);
  3066. }
  3067. }elif (SubMode == LA_UDF_COLLECTION){
  3068. laProp **PreList = 0; void *ThisDBInst = 0;
  3069. NumItems = la_ReadInt(udf);
  3070. VariableNodeSize = la_ReadInt(udf);
  3071. VariableNodeSize = (VariableNodeSize == LA_UDF_VARIABLE_NODE_SIZE) ? 1 : 0;
  3072. la_ReadShort(udf); //active mark
  3073. ActiveInstance = la_ReadPointer(udf);
  3074. if (NumItems == 0) break;
  3075. if (p->UDFNoCreate){
  3076. laPropIterator PI = {0};
  3077. ItemType = la_ReadShort(udf);
  3078. Instance = laGetInstance(p, pp->LastPs->UseInstance, &PI);
  3079. la_ResetInstance(Instance, p->SubProp,0);
  3080. if (ItemType == LA_UDF_HYPER_ITEM){
  3081. if (p->SubProp->Hyper == 2){
  3082. if (!IsExceptionNode) la_ReadHyperData(udf, Instance);
  3083. else la_ReadHyperData(udf, 0);
  3084. if(mUDF) memAssignRef(Instance, &((laMemNodeHyper*)memGetHead(Instance, 0))->FromFile, mUDF);
  3085. memMarkClean(Instance);
  3086. }else la_ReadHyperData(udf, 0);
  3087. if(Mode==LA_UDF_MODE_APPEND){ lstAppendPointer(&MAIN.RenewHyper2s,Instance); }
  3088. }
  3089. if (p->SubProp->PostRead) la_AddPostReadNode(Instance, p->SubProp->PostRead);
  3090. ReadInstance = la_ReadPointer(udf);
  3091. ReadState = la_ReadInt(udf);
  3092. ThisDBInst=Instance;
  3093. PropNumPerItem = la_ReadShort(udf);
  3094. for (j = 0; j < PropNumPerItem; j++){
  3095. int result;
  3096. la_ReadString(udf, buf);
  3097. subp = la_PropLookup(&p->SubProp->Props, buf);
  3098. SubPP.RawThis = pp;
  3099. SubPS.p = subp;
  3100. SubPS.UseInstance = Instance;
  3101. if ((!subp) ||subp->UDFIgnore){la_ExtractFakeProp(udf); continue;}
  3102. if (subp&&!subp->SubProp){ subp->SubProp = la_ContainerLookup(((laSubProp *)subp)->TargetID); }
  3103. result = (IsExceptionNode||!subp) ? result = la_ExtractFakeProp(udf) : la_ExtractProp(udf, mUDF, &SubPP, ThisDBInst, Mode);
  3104. EStatus = result ? result : EStatus;
  3105. }
  3106. if (p->SubProp->PostReadIm) p->SubProp->PostReadIm(Instance);
  3107. }else{
  3108. if(Mode==LA_UDF_MODE_APPEND && p->UDFIsSingle){
  3109. logPrint("[Note] Mode is APPEND but property '%s' only allows one instance, will overwrite.\n", p->Identifier);
  3110. }
  3111. for (i = 0; i < NumItems; i++){
  3112. int RealSize=0; laUDF *SaveUDF = 0; int SaveItemType = 0;IsExceptionNode = 0; Instance=0;
  3113. laPropContainer* pc=p->SubProp;
  3114. if(VariableNodeSize){ la_ReadString(udf, NodeType); pc=la_ContainerLookup(NodeType);
  3115. if(!pc){pc=p->SubProp;} RealSize=pc->NodeSize;
  3116. }
  3117. ItemType = la_ReadShort(udf);
  3118. if(p->UDFReadProgress){
  3119. #ifdef LAGUI_ANDROID // Unbind GLES context so progress can be drawn in the thread.
  3120. eglMakeCurrent(MAIN.egl_dpy,0,0,0);
  3121. #endif
  3122. laShowProgress((real)i/NumItems,-1);
  3123. } //printf("add pc %s\n",pc->Identifier);
  3124. if(pc==LA_PC_SOCKET_OUT || pc==LA_PC_SOCKET_IN){ laGraphRequestRebuild(); }
  3125. int replaced=0;
  3126. if (udf){
  3127. int IsItem=((laSubProp*)p)->ListHandleOffset?1:0;
  3128. RealSize = RealSize ? RealSize : p->SubProp->NodeSize;
  3129. if (!IsExceptionNode){
  3130. if (p->UDFIsSingle && (pp->EndInstance=laGetActiveInstanceStrict(pp->LastPs->p,pp->LastPs->UseInstance))){
  3131. Instance = pp->EndInstance; la_ResetInstance(Instance, pc, IsItem); replaced=1;
  3132. }
  3133. else{
  3134. // if overwrite, find the instance here for hyper2, if not Hyper 2 then notice can't overwrite.
  3135. if (pc->Hyper == 2){
  3136. if(Mode==LA_UDF_MODE_OVERWRITE && ItemType == LA_UDF_HYPER_ITEM){
  3137. laUID uid; la_PeekHyperUID(udf, &uid.String);
  3138. Instance = la_GetReadDBInstNUID(uid.String);
  3139. if(Instance){ la_ResetInstance(Instance, pc,IsItem); replaced=1; }
  3140. else{ /*logPrint("[Note] Hyper2 item [%s] from property '%s' hasn't been loaded yet, will append.\n", uid.String, p->Identifier);*/ }
  3141. }
  3142. if(!Instance) Instance = memAcquireHyperNoAppend(RealSize);
  3143. memMarkClean(Instance);
  3144. }elif (pc->Hyper == 1) Instance = memAcquire(RealSize);
  3145. else Instance = memAcquireSimple(RealSize);
  3146. }
  3147. }
  3148. if (ItemType == LA_UDF_HYPER_ITEM){
  3149. if (pc->Hyper == 2){
  3150. la_ReadHyperData(udf, Instance);
  3151. if(mUDF) memAssignRef(Instance, &((laMemNodeHyper*)memGetHead(Instance, 0))->FromFile, mUDF);
  3152. }
  3153. else la_ReadHyperData(udf, 0);
  3154. if(Mode==LA_UDF_MODE_APPEND){ lstAppendPointer(&MAIN.RenewHyper2s,Instance); }
  3155. }
  3156. if (!IsExceptionNode){
  3157. if (pc->PostRead) la_AddPostReadNode(Instance, pc->PostRead);
  3158. }
  3159. ReadInstance = la_ReadPointer(udf);
  3160. ReadState = la_ReadInt(udf);
  3161. if (!IsExceptionNode && !replaced){
  3162. la_AddDataInst(ReadInstance, pc->Hyper == 2 ? ((laMemNodeHyper *)memGetHead(Instance,0))->NUID.String : 0, Instance);
  3163. }
  3164. ThisDBInst = Instance;
  3165. PropNumPerItem = la_ReadShort(udf);
  3166. for (j = 0; j < PropNumPerItem; j++){
  3167. u64bit ThisSeek;
  3168. la_ReadString(udf, buf);
  3169. subp = la_PropLookup(&pc->Props, buf);
  3170. if ((!subp) || subp->UDFIgnore) la_ExtractFakeProp(udf);
  3171. else{
  3172. int result;
  3173. ThisSeek = la_Tell(udf);
  3174. SubPP.RawThis = pp; SubPS.p = subp; SubPS.UseInstance = Instance;
  3175. la_EnsureSubTarget(subp, 0);
  3176. result = IsExceptionNode ? la_ExtractFakeProp(udf) : la_ExtractProp(udf, mUDF, &SubPP, ThisDBInst, Mode);
  3177. EStatus = result ? result : EStatus;
  3178. }
  3179. }
  3180. if (pc->PostReadIm) pc->PostReadIm(Instance);
  3181. if (!IsExceptionNode && !replaced){
  3182. if (pp->LastPs->UseInstance){
  3183. if (!p->UDFIsSingle){
  3184. if (((laSubProp *)p)->ListHandleOffset){
  3185. laListHandle* inst_lst=(BYTE *)pp->LastPs->UseInstance + ((laSubProp *)p)->ListHandleOffset;
  3186. if(p->Container->SaverDummy && p==p->Container->SaverDummy){ laPurgeSaverDummy(pp->LastPs->UseInstance,p); }
  3187. lstAppendItem(inst_lst, Instance);
  3188. }else{ lstAppendItem(&pc->FailedNodes, Instance); EStatus = 1; }
  3189. }else{ if (!p->UDFNoCreate){ laSetActiveInstance(p, pp->LastPs->UseInstance, Instance); } }
  3190. if (ReadInstance == ActiveInstance) laSetActiveInstance(p, pp->LastPs->UseInstance, Instance);
  3191. }else{
  3192. if (!p->UDFIsSingle){ lstAppendItem(&pc->FailedNodes, Instance); EStatus = 1; }
  3193. }
  3194. }
  3195. }
  3196. }
  3197. }
  3198. if (PreList) memFree(PreList);
  3199. }
  3200. break;
  3201. case LA_PROP_INT:
  3202. case LA_PROP_ARRAY | LA_PROP_INT:
  3203. ReadI = la_ReadIntProp(udf, pp);
  3204. break;
  3205. case LA_PROP_FLOAT:
  3206. case LA_PROP_ARRAY | LA_PROP_FLOAT:
  3207. ReadF = la_ReadFloatProp(udf, pp);
  3208. break;
  3209. case LA_PROP_STRING:
  3210. la_ReadStringProp(udf, pp);
  3211. break;
  3212. case LA_PROP_ENUM:
  3213. case LA_PROP_ARRAY | LA_PROP_ENUM:
  3214. la_ReadEnumProp(udf, pp, buf);
  3215. break;
  3216. case LA_PROP_RAW:
  3217. la_ReadRawProp(udf, pp);
  3218. break;
  3219. default:
  3220. break;
  3221. }
  3222. return EStatus;
  3223. }
  3224. void la_RematchPointers(int Mode){
  3225. laUDFPostRead *uprd;
  3226. void* inst;
  3227. la_ExecutePtrSyncCommand(Mode);
  3228. while(uprd=lstPopItem(&MAIN.PostReadNodes)){ uprd->Func(uprd->Instance); memFree(uprd); }
  3229. while(inst=lstPopPointer(&MAIN.RenewHyper2s)){ laMemNodeHyper* h=memGetHead(inst,0);memMakeHyperData(h);memAssignRef(h,&h->FromFile,0); }
  3230. }
  3231. int la_WriteHyperRecords(laUDF *udf){
  3232. int i = 0;
  3233. u64bit CountSeek = la_Tell(udf);
  3234. u64bit EndSeek;
  3235. laUDFHyperRecordItem *hri;
  3236. laMemNodeHyper* h;
  3237. la_WriteLong(udf, 0);
  3238. while (hri = lstPopItem(&udf->HyperRecords)){
  3239. h=memGetHead(hri->HyperUserMem, 0);
  3240. la_WriteString(udf, h->NUID.String);
  3241. if(hri->pc) la_WriteString(udf, hri->pc->Identifier); else la_WriteString(udf, "");
  3242. la_WritePointer(udf, hri->Seek);
  3243. i++; memFree(hri);
  3244. }
  3245. EndSeek = la_Tell(udf);
  3246. la_Seek(udf, CountSeek); la_WriteLong(udf, i); la_Seek(udf, EndSeek);
  3247. return i;
  3248. }
  3249. void la_WritePropQuickSeek(laUDF *udf){
  3250. for(laUDFPropSegment* ps=udf->PropsToOperate.pFirst;ps;ps=ps->Item.pNext){
  3251. la_WriteString(udf,SSTR(ps->Path)); la_WriteLong(udf,ps->WriteQuickSeek);
  3252. }
  3253. }
  3254. int laPackUDF(laUDF *udf, int UseInstanceList, int DoBackup){
  3255. laUDFPropSegment *ps;
  3256. short NumSegments = 0;
  3257. u64bit RefPos;
  3258. u64bit nuidSeekRef;
  3259. u64bit nuidActualSeek;
  3260. char FilePath[PATH_MAX];
  3261. char OldPath[PATH_MAX];
  3262. char OldPathSwap[PATH_MAX];
  3263. if(DoBackup){
  3264. char BackupPath[1024]; sprintf(BackupPath,"%s~",SSTR(udf->FileName));
  3265. if(laCopyFile(BackupPath,SSTR(udf->FileName))){
  3266. logPrintNew("UDF backup written to \"%s\".\n", BackupPath);
  3267. }else{
  3268. logPrintNew("Error trying to back up \"%s\".\n", SSTR(udf->FileName));
  3269. }
  3270. }
  3271. #ifdef LAGUI_ANDROID
  3272. if(!laAndroidEnsureValidFilePath(SSTR(udf->FileName),W_OK)){ logPrintNew("Can't open \"%s\" on android\n",SSTR(udf->FileName)); return 0; }
  3273. strSafeSet(&udf->FileName, MAIN.AndroidLastPath);
  3274. logPrintNew(MAIN.AndroidLastPath);
  3275. #endif
  3276. sprintf(OldPath,"%s",SSTR(udf->FileName));
  3277. sprintf(FilePath,"%s.incomplete",SSTR(udf->FileName));
  3278. udf->DiskFile = fopen(FilePath, "wb");
  3279. if (!udf->DiskFile) return 0;
  3280. la_WriteOnlyMBString(udf, LA_UDF_IDENTIFIER);
  3281. u64bit SizeAndExt=la_Tell(udf);
  3282. // reserved for extension switches and file size verifications.
  3283. la_WritePointer(udf, 0);
  3284. RefPos = la_Tell(udf);
  3285. la_WriteLong(udf, 0); //How Many Refs.
  3286. la_WriteShort(udf, udf->NumSegmets);
  3287. la_WriteLong(udf, LA_UDF_NUID_SEEK);
  3288. nuidSeekRef = la_Tell(udf);
  3289. la_WritePointer(udf, 0); //Seek pos for nuid list;
  3290. logPrintNew("Packing %s:\n", SSTR(udf->FileName));
  3291. udf->CurrentH2Instance=udf->H2Instances.pFirst;
  3292. for(laUDFPropSegment* ps=udf->PropsToOperate.pFirst;ps;ps=ps->Item.pNext){
  3293. printf(" %-15s\n", ps->Path ? ps->Path->Ptr : ps->PPP->LastPs->p->Identifier);
  3294. ps->WriteQuickSeek = la_Tell(udf);
  3295. la_WriteProp(udf, ps->PPP ? ps->PPP : &ps->PP, ps->PPP ? 1 : 0, UseInstanceList);
  3296. }
  3297. nuidActualSeek = la_Tell(udf);
  3298. la_WritePropQuickSeek(udf);
  3299. la_WriteHyperRecords(udf);
  3300. u64bit ActualSize=la_Tell(udf);
  3301. la_Seek(udf, RefPos); la_WriteLong(udf, udf->TotalRefs);
  3302. la_Seek(udf, nuidSeekRef); la_WritePointer(udf, nuidActualSeek);
  3303. la_Seek(udf, SizeAndExt); la_WritePointer(udf, ((ActualSize&(0x0000ffffffffffff))<<16)|LA_UDF_EXTENSIONS_ENABLED);
  3304. udf->Modified = 0;
  3305. while(ps = lstPopItem(&udf->PropsToOperate)){
  3306. la_FreePropStepCache(ps->PP.Go);
  3307. strSafeDestroy(&ps->Path);
  3308. FreeMem(ps);
  3309. }
  3310. laCloseUDF(udf);
  3311. /* Move disk file to final name and remove the old file. */
  3312. sprintf(OldPathSwap,"%s.swap",OldPath);
  3313. if(rename(OldPath,OldPathSwap)){ logPrint("Failed to rename old file %s\n",OldPath); }
  3314. if(rename(FilePath,OldPath)){ logPrint("Failed to rename file to final name: %s\n",OldPath); }
  3315. elif(remove(OldPathSwap)){ logPrint("Failed to remove old swap file %s\n",OldPathSwap); }
  3316. logPrint("[ALL DONE]\n");
  3317. laHideProgress();
  3318. return 1;
  3319. }
  3320. void laExtractProp(laUDF *udf, char* Path){
  3321. laUDFPropSegment *ps = CreateNew(laUDFPropSegment);
  3322. strSafeSet(&ps->Path, Path);
  3323. }
  3324. int la_PropIsSelected(laUDF* udf, char* Path){
  3325. if(!udf->PropsToOperate.pFirst) return 1;
  3326. for(laUDFPropSegment* ps=udf->PropsToOperate.pFirst;ps;ps=ps->Item.pNext){
  3327. if(strSame(SSTR(ps->Path),Path)) return 1;
  3328. }
  3329. return 0;
  3330. }
  3331. int laExtractUDF(laUDF *udf, laManagedUDF* mUDF, int Mode){
  3332. char Identifier[9] = {0};
  3333. char buf[1024]={0};
  3334. short Version, NumSegments;
  3335. //laPropStep SubPS = { 0 };
  3336. int PointerBits;
  3337. laPropPack SubPP = {0};
  3338. int i;
  3339. int EStatus = 0;
  3340. u64bit SeekRef;
  3341. int IsPart;
  3342. ma_device_stop(&MAIN.Audio->AudioDevice);
  3343. if(MAIN.InitArgs.HasAudio) laSpinLock(&MAIN.Audio->AudioStatusLock);
  3344. MAIN.IsReadingUDF = 1;
  3345. la_ReadBuffer(udf, sizeof(LA_UDF_IDENTIFIER) - 1, Identifier);
  3346. // file size verification and extension switches.
  3347. u64bit verification=la_ReadPointer(udf);
  3348. u64bit FileSize=verification>>16; int Extensions=verification&LA_UDF_ALL_EXTENSIONS;
  3349. if(FileSize && FileSize<la_FileSize(udf)){ logPrintNew("UDF verification failed for %s\n", udf->FileName->Ptr); return 0; }
  3350. /*udf->TotalRefs = */ la_ReadLong(udf);
  3351. NumSegments = la_ReadShort(udf);
  3352. la_ReadLong(udf); //seek mark
  3353. SeekRef = la_ReadPointer(udf);
  3354. logPrintNew("Extracting UDF [0x%x] %s:\n", udf->Extensions, udf->FileName->Ptr);
  3355. //MAIN.NextPendingPointer = 0;
  3356. //MAIN.PendingPointers = CreateNewBuffer(laUDFPointerRecord, udf->TotalRefs);
  3357. //SubPP.LastPs = &SubP;
  3358. for (i = 0; i < NumSegments; i++){
  3359. void *dbi = 0;
  3360. int result, LastOffset, selected=0;
  3361. la_ReadString(udf, buf);
  3362. LastOffset = strlen(buf) - 1;
  3363. buf[LastOffset] = buf[LastOffset] == U'.' ? 0 : buf[LastOffset];
  3364. logPrint(" Prop Segment \"%s\" ...", buf);
  3365. selected = la_PropIsSelected(udf, buf);
  3366. la_GetPropFromPath(&SubPP, 0, buf, 0);
  3367. la_StepPropPack(&SubPP);
  3368. dbi = SubPP.EndInstance; //la_GetWriteDBInst(SubPP.EndInstance);
  3369. result = (SubPP.LastPs&&SubPP.LastPs->p&&selected)?
  3370. la_ExtractProp(udf, mUDF, &SubPP, dbi, Mode):
  3371. la_ExtractFakeProp(udf);
  3372. EStatus = result ? result : EStatus;
  3373. //if(SubPP.LastPs->p->PropertyType!=LA_PROP_RAW){
  3374. // laNotifyUsersPP(&SubPP);
  3375. //}
  3376. logPrint(" [Done]\n", buf);
  3377. la_FreePropStepCache(SubPP.Go);
  3378. SubPP.Go = 0;
  3379. }
  3380. la_RematchPointers(Mode);
  3381. laHideProgress();
  3382. MAIN.IsReadingUDF = 0;
  3383. laUDFPropSegment* ps; while (ps = lstPopItem(&udf->PropsToOperate)){ strSafeDestroy(&ps->Path); FreeMem(ps); }
  3384. if(MAIN.InitArgs.HasAudio) laSpinUnlock(&MAIN.Audio->AudioStatusLock);
  3385. return EStatus;
  3386. }
  3387. int laExtractQuickRaw(FILE* fp, char* path, uint8_t** buf, size_t* size){
  3388. laUDF _udf={0},*udf=&_udf; udf->DiskFile = fp;
  3389. char str[128];
  3390. char Identifier[32];
  3391. la_Seek(udf,0);
  3392. la_ReadBuffer(udf, sizeof(LA_UDF_IDENTIFIER) - 1, Identifier);
  3393. u64bit verification=la_ReadPointer(udf);
  3394. u64bit FileSize=verification>>16; int Extensions=verification&LA_UDF_ALL_EXTENSIONS;
  3395. if(FileSize && FileSize<la_FileSize(udf)){ return 0; }
  3396. if(!(Extensions & LA_UDF_EXTENSION_QUICK_SEEK)){ return 0; }
  3397. /*udf->TotalRefs = */ la_ReadLong(udf);
  3398. short NumSegments = la_ReadShort(udf);
  3399. la_ReadLong(udf); //seek mark
  3400. off_t SeekRef = la_ReadPointer(udf);
  3401. la_Seek(udf,SeekRef);
  3402. for(int i=0;i<NumSegments;i++){
  3403. la_ReadString(udf,str);
  3404. int Seek = la_ReadLong(udf);
  3405. if(strSame(str,path)){
  3406. la_Seek(udf,Seek);
  3407. la_ReadString(udf, str); // prop name
  3408. la_ReadShort(udf);// raw mark
  3409. void* data=la_ReadRaw(udf,size);
  3410. *buf=data; if(data){ return 1; }
  3411. break;
  3412. }
  3413. }
  3414. return 0;
  3415. }
  3416. laManagedUDF* la_FindManagedUDF(char* FileName){
  3417. for(laManagedUDF* m=MAIN.ManagedUDFs.pFirst;m;m=m->Item.pNext){
  3418. if(m->udf&&m->udf->FileName&&!strcmp(m->udf->FileName->Ptr,FileName)) return m;
  3419. }
  3420. return 0;
  3421. }
  3422. laManagedUDF* la_EnsureManagedUDF(char* FileName, int PutAtTop){
  3423. laManagedUDF* m;
  3424. if(!(m=la_FindManagedUDF(FileName))){
  3425. m=memAcquire(sizeof(laManagedUDF)); strSafeSet(&m->BaseName, strGetLastSegment(FileName,LA_PATH_SEP));
  3426. if(PutAtTop) lstPushItem(&MAIN.ManagedUDFs, m); else lstAppendItem(&MAIN.ManagedUDFs, m);
  3427. }
  3428. return m;
  3429. }
  3430. void la_MakeDummyManagedUDF(){
  3431. MAIN.DummyManageUDF=la_EnsureManagedUDF(transLate("< Save as a new file >"), 1);
  3432. if(!MAIN.DummyManageUDFSingle){
  3433. MAIN.DummyManageUDFSingle=memAcquire(sizeof(laManagedUDF)); strSafeSet(&MAIN.DummyManageUDFSingle->BaseName, transLate("< Choose file >"));
  3434. MAIN.DummyManageUDFSingleForce=memAcquire(sizeof(laManagedUDF)); strSafeSet(&MAIN.DummyManageUDFSingleForce->BaseName, transLate("< Force >"));
  3435. }
  3436. }
  3437. laManagedSaveProp* laSaveProp(char* path){
  3438. if(!path || !path[0]) return 0; laManagedSaveProp* msp=0;
  3439. for(laManagedSaveProp* m=MAIN.ManagedSaveProps.pFirst;m;m=m->Item.pNext){
  3440. if(!strcmp(m->Path->Ptr,path)){ msp=m; break; }
  3441. }
  3442. if(!msp){ msp=memAcquireSimple(sizeof(laManagedSaveProp)); lstAppendItem(&MAIN.ManagedSaveProps, msp); }
  3443. strSafeSet(&msp->Path, path);
  3444. return msp;
  3445. }
  3446. void laSaveAlongside(laManagedSaveProp* parent, char* path){
  3447. if(!path || !path[0] || !parent) return; laManagedSaveProp* msp=0;
  3448. for(laManagedSaveProp* m=parent->SaveAlongside.pFirst;m;m=m->Item.pNext){
  3449. if(!strcmp(m->Path->Ptr,path)){ msp=m; break; }
  3450. }
  3451. if(!msp){ msp=memAcquireSimple(sizeof(laManagedSaveProp)); lstAppendItem(&parent->SaveAlongside, msp); }
  3452. strSafeSet(&msp->Path, path);
  3453. }
  3454. void laSetThumbnailProp(char* path){
  3455. strSafeSet(&MAIN.ThumbnailProp,path);
  3456. }
  3457. void laClearSaveProp(){
  3458. laManagedSaveProp* m; while(m=lstPopItem(&MAIN.ManagedSaveProps)) {
  3459. laManagedSaveProp* ma; while(ma=lstPopItem(&m->SaveAlongside)){ strSafeDestroy(&ma->Path); memFree(ma); }
  3460. strSafeDestroy(&m->Path); memFree(m);
  3461. }
  3462. }
  3463. int la_ScanForModifiedRecursive(laPropPack* pp, int ReturnIfAnyMod, int ReturnIfAnyEmpty, int* rempty, int RegisterToUDF){
  3464. int result=0;
  3465. laProp* p=pp->LastPs->p; laPropIterator pi={0};
  3466. if(p->PropertyType!=LA_PROP_SUB || p->UDFIsRefer || p->UDFIgnore || p->UDFOnly) return 0;
  3467. la_EnsureSubTarget(p, 0); //if(p->SubProp && p->SubProp->Hyper!=2) return 0;
  3468. void* inst = laGetInstance(p, pp->LastPs->UseInstance, &pi);
  3469. while(inst){
  3470. laPropContainer* pc=la_EnsureSubTarget(p, inst);
  3471. if(pc->Hyper!=2){
  3472. laPropPack spp={0}; laPropStep sps={0}; spp.LastPs=&sps;
  3473. for(laProp* sp=pc->Props.pFirst;sp;sp=sp->Item.pNext){
  3474. sps.UseInstance=inst; sps.p=sp;
  3475. if(sp->PropertyType==LA_PROP_SUB) {
  3476. result|=la_ScanForModifiedRecursive(&spp, ReturnIfAnyMod, ReturnIfAnyEmpty, rempty, RegisterToUDF);
  3477. if((ReturnIfAnyMod||ReturnIfAnyEmpty)&&result)return result;
  3478. }
  3479. }
  3480. }else{
  3481. laMemNodeHyper* m = memGetHead(inst,0);
  3482. int DummyClean=((p!=p->Container->SaverDummy)||((p==p->Container->SaverDummy)&&(!m->Modified)));
  3483. if(!m->FromFile || m->FromFile==MAIN.DummyManageUDF){
  3484. if(!DummyClean){
  3485. result|=1; if(rempty)*rempty|=1; if((ReturnIfAnyMod||ReturnIfAnyEmpty)&&result)return result;
  3486. }
  3487. }
  3488. if((!p->UDFIsSingle)&&(!p->UDFNoCreate)&&RegisterToUDF&&m->FromFile&&m->FromFile->udf){ la_IncludeHyper2Instance(m->FromFile->udf, pc, inst); }
  3489. if(m->Modified){
  3490. if(m->FromFile && m->FromFile->udf){ m->FromFile->udf->Modified=1; }
  3491. result|=1; if(ReturnIfAnyMod&&result)return result;
  3492. }
  3493. }
  3494. inst=laGetNextInstance(p,inst,&pi);
  3495. }
  3496. return result;
  3497. }
  3498. int laRegisterModifications(int ReturnIfAnyMod, int ReturnIfAnyEmpty, int* rempty, int RegisterToUDF){
  3499. int result=0, registered; if(RegisterToUDF){ReturnIfAnyMod=ReturnIfAnyEmpty=0;}
  3500. for(laManagedUDF* m=MAIN.ManagedUDFs.pFirst;m;m=m->Item.pNext){
  3501. if(!m->udf)continue;
  3502. m->udf->Modified=0;
  3503. la_ClearHyper2Instances(m->udf);
  3504. }
  3505. for(laManagedSaveProp* msp=MAIN.ManagedSaveProps.pFirst;msp;msp=msp->Item.pNext){
  3506. laPropPack PP={0}; registered=0;
  3507. if(msp->Path&&msp->Path->Ptr&&la_GetPropFromPath(&PP, 0, msp->Path->Ptr, 0)){
  3508. la_StepPropPack(&PP);
  3509. result|=la_ScanForModifiedRecursive(&PP, ReturnIfAnyMod, ReturnIfAnyEmpty, rempty, RegisterToUDF);
  3510. for(laManagedUDF* m=MAIN.ManagedUDFs.pFirst;m;m=m->Item.pNext){
  3511. if(!m->udf)continue;
  3512. if(m->udf->HasInstances){ laWriteProp(m->udf,SSTR(msp->Path));
  3513. for(laManagedSaveProp* am=msp->SaveAlongside.pFirst;am;am=am->Item.pNext){
  3514. laWriteProp(m->udf,SSTR(am->Path));
  3515. }
  3516. m->udf->HasInstances=0;
  3517. }
  3518. }
  3519. if((ReturnIfAnyMod||ReturnIfAnyEmpty)&&result)return result;
  3520. la_FreePropStepCache(PP.Go);
  3521. }
  3522. }
  3523. return result;
  3524. }
  3525. void la_ReadUDFToMemory(laUDF *udf){
  3526. if (udf->FileContent)
  3527. return;
  3528. fseek(udf->DiskFile, 0, SEEK_END);
  3529. u64bit SeekEnd = ftell(udf->DiskFile);
  3530. fseek(udf->DiskFile, 0, SEEK_SET);
  3531. udf->FileContent = calloc(1, SeekEnd);
  3532. udf->FileContentSize=SeekEnd;
  3533. fread(udf->FileContent, SeekEnd, 1, udf->DiskFile);
  3534. udf->Seek = 0;
  3535. }
  3536. void la_ReadOwnHyperItems(laUDF *udf, laUDFRegistry* r){
  3537. int Count, i;
  3538. laUDFOwnHyperItem *ohi;
  3539. Count = la_ReadLong(udf);
  3540. int Seek;
  3541. char name[256]; laUID uid;
  3542. for (i = 0; i < Count; i++){
  3543. la_ReadString(udf, uid.String);
  3544. la_ReadString(udf, name);
  3545. Seek=la_ReadPointer(udf);
  3546. laPropContainer* pc = la_ContainerLookup(name);
  3547. if(!laFindHyperResource(uid.String)){
  3548. ohi = laNewHyperResource(uid.String);
  3549. ohi->Registry = r;
  3550. ohi->Seek = Seek;
  3551. strcpy(ohi->NUID.String, uid.String);
  3552. //logPrint("Found Resource: %s | %s\n",ohi->NUID.String,name);
  3553. } else {logPrint("Duplicated Resource: %s | %s\n",uid.String,name);}
  3554. }
  3555. }
  3556. void la_ReadPropQuickSeek(laUDF *udf){
  3557. char* buf[128];
  3558. for(int i=0;i<udf->NumSegmets;i++){
  3559. laUDFPropQuickSeek *qs = memAcquireSimple(sizeof(laUDFPropQuickSeek));
  3560. la_ReadString(udf,buf); strSafeSet(&qs->Path,buf);
  3561. qs->QuickSeek = la_ReadLong(udf);
  3562. lstAppendItem(&udf->QuickSeeks,qs);
  3563. //logPrint(" %dx QuickSeeks\n",qs->QuickSeek,SSTR(qs->Path));
  3564. }
  3565. }
  3566. laUDF *laOpenUDF(char *FileName, int ReadToMemory, laUDFRegistry* ReadRegistryRef, laManagedUDF** UseManaged){
  3567. char Identifier[9] = {0};
  3568. u64bit SeekRef;
  3569. laUDF *udf;
  3570. u64bit extensions;
  3571. char FilePath[1024]={0};
  3572. udf=memAcquire(sizeof(laUDF));
  3573. udf->DiskFile = fopen(FileName, "rb");
  3574. if (!udf->DiskFile) return 0;
  3575. #ifdef LAGUI_ANDROID
  3576. strSafeSet(&udf->FileName, MAIN.AndroidLastPath);
  3577. #else
  3578. strSafeSet(&udf->FileName, FileName);
  3579. #endif
  3580. if(ReadToMemory){ la_ReadUDFToMemory(udf); fclose(udf->DiskFile); udf->DiskFile = 0; }
  3581. la_ReadBuffer(udf, sizeof(LA_UDF_IDENTIFIER) - 1, Identifier);
  3582. if (!strSame(Identifier, LA_UDF_IDENTIFIER)){ laCloseUDF(udf); logPrintNew("\"%s\" is not a UDF file.\n", FileName); return 0; }
  3583. if(UseManaged){
  3584. laManagedUDF* m=la_EnsureManagedUDF(FileName, 0);
  3585. if(!m->udf) m->udf=udf; udf->Managed=1; *UseManaged=m;
  3586. }
  3587. extensions = la_ReadPointer(udf);
  3588. udf->Extensions = extensions & LA_UDF_ALL_EXTENSIONS;
  3589. if((udf->Extensions | LA_UDF_EXTENSIONS_ENABLED)!=LA_UDF_EXTENSIONS_ENABLED){
  3590. logPrint("Unsupported UDF extension 0x%x (0x%x enabled), it's probably saved with a newer version of the program.\n",
  3591. udf->Extensions,LA_UDF_EXTENSIONS_ENABLED);
  3592. laCloseUDF(udf);
  3593. return 0;
  3594. }
  3595. udf->TotalRefs = la_ReadLong(udf); //total refs
  3596. udf->NumSegmets = la_ReadShort(udf); //num segments
  3597. la_ReadLong(udf); //seek mark
  3598. SeekRef = la_ReadPointer(udf);
  3599. la_Seek(udf, SeekRef);
  3600. if(ReadRegistryRef){
  3601. if(udf->Extensions & LA_UDF_EXTENSION_QUICK_SEEK){
  3602. la_ReadPropQuickSeek(udf);
  3603. }
  3604. la_ReadOwnHyperItems(udf, ReadRegistryRef);
  3605. }
  3606. la_Seek(udf, 0);
  3607. udf->Opened = 1;
  3608. return udf;
  3609. }
  3610. void laCloseUDF(laUDF *udf){
  3611. laUDFOwnHyperItem *ohi;
  3612. laUDFHyperRecordItem *hri;
  3613. laUDFPropSegment *ps;
  3614. laUDFPropQuickSeek *qs;
  3615. if (udf->DiskFile){ fclose(udf->DiskFile); udf->DiskFile=0; };
  3616. while (lstPopPointer(&udf->OwnHyperItems));
  3617. while (lstPopPointer(&udf->HyperRecords));
  3618. while (qs = lstPopItem(&udf->QuickSeeks)){ strSafeDestroy(&qs->Path); memFree(qs); }
  3619. if(udf->CurrentH2Instance){ logPrint("[WARN] udf->CurrentH2Instance!=0 after UDF packing.\n"); udf->CurrentH2Instance=0; }
  3620. udf->NumSegmets=0;
  3621. la_ClearHyper2Instances(udf);
  3622. if(udf->FileContent) FreeMem(udf->FileContent);
  3623. if(!udf->Managed){ strSafeDestroy(&udf->FileName); memFree(udf); }
  3624. }
  3625. int laLoadHyperResources(char* uid_search){
  3626. int count=0;
  3627. logPrintNew("Loading extra resources \"%s\"\n",uid_search);
  3628. for(laUDFOwnHyperItem* ohi=MAIN.UDFResources.pFirst;ohi;ohi=ohi->Item.pNext){
  3629. if(strstr(ohi->NUID.String,uid_search)){ laRequestAdditionalRegistry(ohi->Registry); count++; };
  3630. }
  3631. if(count){ la_LoadAdditionalRegistries(); } return count;
  3632. }
  3633. laUDFOwnHyperItem* laFindHyperResource(char* uid){
  3634. for(laUDFOwnHyperItem* ohi=MAIN.UDFResources.pFirst;ohi;ohi=ohi->Item.pNext){
  3635. if(!strcmp(uid, ohi->NUID.String)) return ohi;
  3636. }
  3637. return 0;
  3638. }
  3639. laUDFOwnHyperItem* laNewHyperResource(char* uid){
  3640. laUDFOwnHyperItem* ohi = memAcquireSimple(sizeof(laUDFOwnHyperItem));
  3641. lstAppendItem(&MAIN.UDFResources, ohi); return ohi;
  3642. }
  3643. laUDFRegistry* laFindUDFRegistry(char* Path){
  3644. for(laUDFRegistry* r=MAIN.ResourceRegistries.pFirst;r;r=r->Item.pNext){
  3645. if(r->Path && !strcmp(Path, r->Path->Ptr)) return r;
  3646. }
  3647. return 0;
  3648. }
  3649. laUDFRegistry* laCreateUDFRegistry(char* Path){
  3650. if(!Path) return 0;
  3651. laUDFRegistry* r = memAcquire(sizeof(laUDFRegistry));
  3652. strSafeSet(&r->Path, Path);
  3653. lstAppendItem(&MAIN.ResourceRegistries, r);
  3654. return r;
  3655. }
  3656. void laRequestAdditionalRegistry(laUDFRegistry* r){
  3657. if(la_FindManagedUDF(r->Path->Ptr)) return;
  3658. if(lstHasPointer(&MAIN.PendingResourceRequests, r)) return;
  3659. logPrint("[INFO] Request additional resources in: %s\n", r->Path->Ptr);
  3660. lstAppendPointer(&MAIN.PendingResourceRequests,r);
  3661. }
  3662. void laClearUDFRegistries(){
  3663. laUDFOwnHyperItem* ohi; laUDFRegistry* r;
  3664. while(ohi=lstPopItem(&MAIN.UDFResources)) memFree(ohi);
  3665. while(r=lstPopItem(&MAIN.ResourceRegistries)){
  3666. strSafeDestroy(&r->Path); memFree(r);
  3667. }
  3668. }
  3669. void laGetSubResourceDirectories(char* rootpath_with_slash, laListHandle* out){
  3670. laSafeString*s=0; strSafePrint(&s, "%sUDFExtra" LA_PATH_SEPSTR, rootpath_with_slash); lstAppendPointer(out,s);
  3671. char Final[1024];
  3672. sprintf(Final, "%s.udfextra",rootpath_with_slash);
  3673. FILE* f=fopen(Final, "r"); if(!f){ return; }
  3674. char dir[1024];
  3675. while(fgets(dir,1024,f)){ laSafeString*s=0; strSafePrint(&s, "%s%s" LA_PATH_SEPSTR, rootpath_with_slash, dir); lstAppendPointer(out,s); }
  3676. fclose(f);
  3677. }
  3678. void laRefreshUDFResourcesIn(char* rootpath){
  3679. char Final[2048];
  3680. int NumFiles=-1;
  3681. int len = strlen(rootpath);
  3682. if (len && rootpath[len - 1] != LA_PATH_SEP) strcat(rootpath, LA_PATH_SEPSTR);
  3683. #if defined(__linux__) || defined(LAGUI_ANDROID)
  3684. struct dirent **NameList=0;
  3685. NumFiles=scandir(rootpath,&NameList,0,alphasort);
  3686. for(int i=0;i<NumFiles;i++){
  3687. struct dirent* d = NameList[i]; int dlen;
  3688. char *format = strGetLastSegment(d->d_name, '.'); int file_okay=0;
  3689. for(laExtensionType* et=MAIN.ExtraExtensions.pFirst;et;et=et->Item.pNext){ if(et->FileType==LA_FILETYPE_UDF && strSame(et->Extension,format)){file_okay=1;break;} }
  3690. if(!file_okay) continue;
  3691. struct stat s;
  3692. sprintf(Final, "%s%s",rootpath,d->d_name);
  3693. stat(Final, &s);
  3694. if (!S_ISDIR(s.st_mode)){
  3695. if(!laFindUDFRegistry(Final)){
  3696. laUDFRegistry* r = laCreateUDFRegistry(Final);
  3697. laUDF* udf = laOpenUDF(Final, 0, r, 0);
  3698. if(udf) laCloseUDF(udf);
  3699. }
  3700. }
  3701. }
  3702. for (int i=0;i<NumFiles;i++){ free(NameList[i]); } if(NameList) free(NameList);
  3703. #endif
  3704. #ifdef _WIN32
  3705. WIN32_FIND_DATA FindFileData;
  3706. HANDLE hFind;
  3707. SYSTEMTIME stUTC, stLocal;
  3708. char Lookup[PATH_MAX];
  3709. strCopyFull(Lookup, rootpath); strcat(Lookup, "*.*");
  3710. hFind = FindFirstFile(Lookup, &FindFileData);
  3711. if (hFind == INVALID_HANDLE_VALUE) return;
  3712. while (1){
  3713. NumFiles++;
  3714. char* format = strGetLastSegment(FindFileData.cFileName, '.'); int file_okay = 0;
  3715. for (laExtensionType* et = MAIN.ExtraExtensions.pFirst; et; et = et->Item.pNext) { if (et->FileType == LA_FILETYPE_UDF && strSame(et->Extension, format)) { file_okay = 1; break; } }
  3716. if (file_okay) {
  3717. sprintf(Final, "%s%s", rootpath, FindFileData.cFileName);
  3718. struct stat s; stat(Final, &s);
  3719. if (!(s.st_mode & S_IFDIR)) {
  3720. if (!laFindUDFRegistry(Final)) {
  3721. laUDFRegistry* r = laCreateUDFRegistry(Final);
  3722. laUDF* udf = laOpenUDF(Final, 0, r, 0);
  3723. if (udf) laCloseUDF(udf);
  3724. }
  3725. }
  3726. }
  3727. if (!FindNextFile(hFind, &FindFileData))
  3728. break;
  3729. }
  3730. #endif
  3731. if (NumFiles >= 0) {
  3732. laListHandle additionals = { 0 }; laSafeString* s;
  3733. laGetSubResourceDirectories(rootpath, &additionals);
  3734. while (s = lstPopPointer(&additionals)) { laRefreshUDFResourcesIn(s->Ptr); strSafeDestroy(&s); }
  3735. }
  3736. }
  3737. void laRefreshUDFRegistries(){
  3738. laClearUDFRegistries();
  3739. char LookupM[PATH_MAX];
  3740. /* Always scan root directory. */
  3741. #ifdef LAGUI_ANDROID
  3742. const char* rootdir="";
  3743. sprintf(LookupM,"%s",rootdir);
  3744. laRefreshUDFResourcesIn(LookupM);
  3745. sprintf(LookupM,"%s/%s",MAIN.ExternalDataPath,rootdir);
  3746. laRefreshUDFResourcesIn(LookupM);
  3747. sprintf(LookupM,"%s/%s",MAIN.InternalDataPath,rootdir);
  3748. laRefreshUDFResourcesIn(LookupM);
  3749. #else
  3750. const char* rootdir=".";
  3751. realpath(rootdir, LookupM);
  3752. laRefreshUDFResourcesIn(LookupM);
  3753. #endif
  3754. for(laResourceFolder* rf = MAIN.ResourceFolders.pFirst;rf;rf=rf->Item.pNext){
  3755. #ifdef LAGUI_ANDROID
  3756. sprintf(LookupM,"%s",SSTR(rf->Path));
  3757. laRefreshUDFResourcesIn(LookupM);
  3758. sprintf(LookupM,"%s/%s",MAIN.ExternalDataPath,SSTR(rf->Path));
  3759. laRefreshUDFResourcesIn(LookupM);
  3760. sprintf(LookupM,"%s/%s",MAIN.InternalDataPath,SSTR(rf->Path));
  3761. laRefreshUDFResourcesIn(LookupM);
  3762. #else
  3763. realpath(SSTR(rf->Path), LookupM);
  3764. laRefreshUDFResourcesIn(LookupM);
  3765. #endif
  3766. }
  3767. }
  3768. void laStopManageUDF(laManagedUDF* m){
  3769. if(!m) return;
  3770. lstRemoveItem(&MAIN.ManagedUDFs,m);
  3771. if(m->udf) { m->udf->Managed=0; laCloseUDF(m->udf); };
  3772. strSafeDestroy(&m->BaseName);
  3773. memFree(m);
  3774. }
  3775. void laClearManagedUDF(){
  3776. laManagedUDF* m;
  3777. while(m=MAIN.ManagedUDFs.pFirst){ laStopManageUDF(m); }
  3778. }
  3779. int laSaveManagedUDF(int ModifiedOnly){
  3780. int allsuccess=1;
  3781. laRegisterModifications(0,0,0,1);
  3782. if(MAIN.PreSave){ MAIN.PreSave(); }
  3783. for(laManagedUDF* m=MAIN.ManagedUDFs.pFirst;m;m=m->Item.pNext){
  3784. if(!m->udf) continue;
  3785. if(ModifiedOnly && !m->udf->Modified){ continue; }
  3786. if(m->udf->PropsToOperate.pFirst){ if(!laPackUDF(m->udf, 1, 1)){ allsuccess=0; }; }
  3787. laCloseUDF(m->udf);// just in case
  3788. }
  3789. if(MAIN.PostSave){ MAIN.PostSave(); }
  3790. return allsuccess;
  3791. }
  3792. void laPropagateUDF(laPropContainer* pc, void* inst, int force){
  3793. if(!pc->UDFPropagate) return;
  3794. void* udf=laget_InstanceActiveUDF(inst); if(udf==MAIN.DummyManageUDF) return;
  3795. pc->UDFPropagate(inst, udf, force);
  3796. }
  3797. void laSetSaveCallback(laPreSaveF PreSave, laPostSaveF PostSave){
  3798. MAIN.PreSave=PreSave;
  3799. MAIN.PostSave=PostSave;
  3800. }
  3801. //==========================================================================[Manifest]
  3802. void laAddResourceFolder(char* Path){
  3803. laResourceFolder* rf=memAcquire(sizeof(laResourceFolder));
  3804. if(Path) strSafeSet(&rf->Path, Path);
  3805. lstAppendItem(&MAIN.ResourceFolders, rf);
  3806. }
  3807. void laRemoveResourceFolder(laResourceFolder* rf){
  3808. strSafeDestroy(&rf->Path);
  3809. lstRemoveItem(&MAIN.ResourceFolders, rf);
  3810. memFree(rf);
  3811. }
  3812. void la_ClearUDFRegistryAndFolders(){
  3813. laResourceFolder* rf; while(rf=MAIN.ResourceFolders.pFirst){ laRemoveResourceFolder(rf); }
  3814. laClearUDFRegistries();
  3815. }
  3816. //==========================================================================[undo]
  3817. void laPushDifferenceOnly(char* Description, u64bit hint){
  3818. laDiff* d=memAcquire(sizeof(laDiff));
  3819. lstAppendItem(&MAIN.Differences, d);
  3820. if(MAIN.HeadDifference && Description) strSafeSet(&MAIN.HeadDifference->Description,Description);
  3821. d->Hint=hint; MAIN.HeadDifference=d;
  3822. laNotifyUsers("la.differences");
  3823. }
  3824. void laPushDifferences(char* Description, u64bit hint){
  3825. memFreeRemainingLeftNodes();
  3826. laPushDifferenceOnly(Description,hint);
  3827. }
  3828. void la_FreeInstance(void* inst, laPropContainer* pc, int no_free);
  3829. void la_FreeDBInst(laDBInst* dbi, int no_freeinst, int cleanup_only, int SkipInstances);
  3830. void la_FreeDBProp(laDBProp* dbp, int cleanup_only, int SkipInstances){
  3831. //printf("free dbp %s %x\n",dbp->p->Identifier,dbp);
  3832. if(dbp->p->PropertyType==LA_PROP_SUB){
  3833. if((((laSubProp*)dbp->p)->ListHandleOffset||dbp->p->UDFNoCreate||dbp->p->UDFIsSingle)&&(!dbp->p->UDFIsRefer)&&(!SkipInstances)){
  3834. laDBSubProp* dsp=dbp; laDBInst* si;
  3835. //printf("fdbp %s %x %x %x\n",dbp->p->Identifier,dsp->Instances.pFirst,dsp->Instances.pLast,((laListItem*)dsp->Instances.pFirst)->pNext);
  3836. while(si=lstPopItem(&dsp->Instances)){ la_FreeDBInst(si,dbp->p->UDFNoCreate||(!dbp->p->OffsetIsPointer),cleanup_only,SkipInstances); }
  3837. } // prevent freeing the data;
  3838. }elif(dbp->p->PropertyType==LA_PROP_STRING && ((laStringProp*)dbp->p)->IsSafeString){
  3839. strSafeSet(&dbp->Data,0);
  3840. }elif(dbp->p->PropertyType==LA_PROP_RAW){
  3841. //printf("raw dbp %s\n",dbp->p->Identifier);
  3842. free(dbp->Data);
  3843. }else{
  3844. //printf("-data- %x\n",dbp->Data);
  3845. memFree(dbp->Data);
  3846. }
  3847. memFree(dbp);
  3848. }
  3849. void la_FreeDBInst(laDBInst* dbi, int no_freeinst, int cleanup_only, int SkipInstances){
  3850. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstLink,dbi->OriginalInstance); lstRemoveItem(l, &dbi->Item2);
  3851. //printf("free dbi %s %x\n", dbi->pc->Identifier,dbi);
  3852. laDBProp* dbp; while(dbp=lstPopItem(&dbi->Props)){ la_FreeDBProp(dbp, cleanup_only,SkipInstances); }
  3853. if(dbi->OriginalInstance && (!cleanup_only)) la_FreeInstance(dbi->OriginalInstance, dbi->pc, no_freeinst);
  3854. memFree(dbi);
  3855. }
  3856. void la_FreeDiffCommand(laDiffCommand* dc, laDiff* d, int FromLeft){
  3857. //printf("freedc %s\n",dc->p->Identifier);
  3858. if(dc->p->PropertyType==LA_PROP_SUB && (((laSubProp*)dc->p)->ListHandleOffset||dc->p->UDFNoCreate||dc->p->UDFIsSingle) && (!dc->p->UDFIsRefer)){
  3859. laDiffCommandInst* dci; laDiffCommandSub* dcs=dc;
  3860. while(dci=lstPopItem(&dcs->AddedInst)){ if(!FromLeft) la_FreeDBInst(dci->DBInst,(dc->p->UDFNoCreate||(!dc->p->OffsetIsPointer)),0,0); memFree(dci); }
  3861. while(dci=lstPopItem(&dcs->MovedInst)){ memFree(dci); }
  3862. while(dci=lstPopItem(&dcs->RemovedInst)){ if(FromLeft) la_FreeDBInst(dci->DBInst,(dc->p->UDFNoCreate||(!dc->p->OffsetIsPointer)),0,1); memFree(dci); }
  3863. }elif(dc->p->PropertyType==LA_PROP_STRING && ((laStringProp*)dc->p)->IsSafeString){
  3864. strSafeSet(&dc->Data,0);
  3865. }elif(dc->p->PropertyType==LA_PROP_RAW){
  3866. //printf("raw %s\n",dc->p->Identifier);
  3867. free(dc->Data);
  3868. }
  3869. memFree(dc);
  3870. }
  3871. void la_FreeDifference(laDiff* d, int FromLeft){
  3872. laDiffCommand* dc; laDiffCommandCustom* dcc; laDiffExtraTouched* det;
  3873. while(dc=lstPopItem(&d->Commands)){ la_FreeDiffCommand(dc,d,FromLeft); }
  3874. while(dcc=lstPopItem(&d->CustomCommands)){ if(dcc->Free) dcc->Free(dcc->Data, FromLeft); }
  3875. while(det=lstPopItem(&d->ExtraTouched)){ memFree(det); }
  3876. //if(d->Description&&d->Description->Ptr){printf("%s\n",d->Description->Ptr);}
  3877. strSafeDestroy(&d->Description);
  3878. }
  3879. void laFreeNewerDifferences(){
  3880. laDiff* PrevD;
  3881. if(MAIN.HeadDifference==MAIN.Differences.pLast) return;
  3882. for(laDiff* d=MAIN.Differences.pLast;d;d=PrevD){
  3883. PrevD=d->Item.pPrev;
  3884. lstRemoveItem(&MAIN.Differences,d);
  3885. la_FreeDifference(d,0);
  3886. if(MAIN.HeadDifference==d){ MAIN.HeadDifference=PrevD; laPushDifferenceOnly(0,0); break; }
  3887. }
  3888. }
  3889. void laFreeOlderDifferences(int KeepHowMany){
  3890. laDiff* endd; int count=0;
  3891. for(endd=MAIN.HeadDifference;endd;endd=endd->Item.pPrev){
  3892. if(count>=KeepHowMany) break; count++;
  3893. }
  3894. if(!endd) return; laDiff* d,*NextD;
  3895. while(d=lstPopItem(&MAIN.Differences)){
  3896. NextD=MAIN.Differences.pFirst;
  3897. la_FreeDifference(d,1);
  3898. if(MAIN.HeadDifference==d){ MAIN.HeadDifference=NextD; laPushDifferenceOnly(0,0); break; }
  3899. if(d==endd){ break; }
  3900. }
  3901. }
  3902. void la_FreeAllDifferences(){
  3903. laFreeNewerDifferences();
  3904. laFreeOlderDifferences(1);
  3905. }
  3906. void la_NoLongerRecordUndo(){
  3907. la_FreeAllDifferences();
  3908. laDBProp*dbp; while(dbp=lstPopItem(&MAIN.RootDBInst.Props)){ la_FreeDBProp(dbp,1,0); }
  3909. laDBRecordedProp* p; while(p=lstPopItem(&MAIN.DBRecordedProps)){ strSafeDestroy(&p->OriginalPath); memFree(p); }
  3910. hshFree(&MAIN.DBInstLink);
  3911. }
  3912. void la_RelinkDBInst(laDBInst* dbi, void* New_OriginalInstance){
  3913. if(dbi->OriginalInstance){
  3914. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstLink,dbi->OriginalInstance); lstRemoveItem(l, &dbi->Item2);
  3915. }
  3916. dbi->OriginalInstance = New_OriginalInstance;
  3917. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstLink,dbi->OriginalInstance); lstPushItem(l, &dbi->Item2);
  3918. }
  3919. laDBInst* laAddDBInst(laDBProp* parent, void* inst, laPropContainer* pc, laDiff* Begin){
  3920. laDBInst* dbi=memAcquire(sizeof(laDBInst)); if(!Begin) Begin=MAIN.HeadDifference;
  3921. dbi->pc=pc;
  3922. if(parent){ lstAppendItem(&((laDBSubProp*)parent)->Instances, dbi); }
  3923. la_RelinkDBInst(dbi, inst);
  3924. return dbi;
  3925. }
  3926. laDBProp* laAddDBProp(laDBInst* dbi, laProp* p, void* data){
  3927. int size=sizeof(laDBProp);
  3928. if(p->PropertyType==LA_PROP_SUB && (!p->UDFIsRefer)){size=sizeof(laDBSubProp);}
  3929. elif(p->PropertyType==LA_PROP_RAW){size=sizeof(laDBRawProp);}
  3930. laDBProp* dbp=memAcquire(size);
  3931. dbp->p=p; dbp->Data=data;
  3932. lstAppendItem(&dbi->Props, dbp);
  3933. return dbp;
  3934. }
  3935. void laAddDBPReferAcquire(laDBProp* dbp){
  3936. if(!dbp->Data) return; if(!lstHasPointer(&MAIN.DBInstPendingAcquireDBP, dbp)) lstAppendPointer(&MAIN.DBInstPendingAcquireDBP, dbp);
  3937. }
  3938. void laAddDiffCMDReferAcquire(laDiffCommand* ds){
  3939. if(!ds->Data) return; if(!lstHasPointer(&MAIN.DBInstPendingAcquireDiffCMD, ds)) lstAppendPointer(&MAIN.DBInstPendingAcquireDiffCMD, ds);
  3940. }
  3941. laDiffCommand* la_GiveDiffCommand(laDiff* diff, laDBInst* Instance, laProp* p, void* Data){
  3942. for(laDiffCommand* dc=diff->Commands.pFirst;dc;dc=dc->Item.pNext){
  3943. if(dc->Instance == Instance && dc->p == p) return dc;
  3944. }
  3945. int size=sizeof(laDiffCommand);
  3946. if(p->PropertyType==LA_PROP_SUB && (!(p->UDFIsRefer||p->UDFIsSingle||p->UDFNoCreate))){size=sizeof(laDiffCommandSub);}
  3947. elif(p->PropertyType==LA_PROP_RAW){size=sizeof(laDiffCommandRaw);}
  3948. laDiffCommand* dc = memAcquire(size);
  3949. dc->Instance=Instance; dc->p=p; dc->Data = Data;
  3950. lstAppendItem(&diff->Commands,dc);
  3951. return dc;
  3952. }
  3953. void la_GiveExtraTouched(laDiff* diff, laDBInst* dbi){
  3954. if(!dbi->pc->UndoTouched) return;
  3955. laDiffExtraTouched* det=memAcquire(sizeof(laDiffExtraTouched));
  3956. det->dbi=dbi; lstAppendItem(&diff->ExtraTouched,det);
  3957. }
  3958. int la_AddIntDBProp(laDBInst* dbi, laDBProp* dbp, laDiff* diff, laPropPack *pp){
  3959. laProp *p = pp->LastPs->p; int *Data; int len = laGetArrayLength(pp); int ret=0;
  3960. Data= memAcquireSimple(sizeof(int)*len);
  3961. laGetIntArray(pp, Data); if(diff&&dbp){
  3962. if(memcmp(dbp->Data, Data, sizeof(int)*len)){ la_GiveDiffCommand(diff, dbi, p, dbp->Data); dbp->Data=Data; ret=1; } else memFree(Data);
  3963. }
  3964. else laAddDBProp(dbi, p, Data);
  3965. return ret;
  3966. }
  3967. int la_AddFloatDBProp(laDBInst* dbi, laDBProp* dbp, laDiff* diff, laPropPack *pp){
  3968. laProp *p = pp->LastPs->p; real *Data; int len = laGetArrayLength(pp); int ret=0;
  3969. Data= memAcquireSimple(sizeof(real)*len);
  3970. laGetFloatArray(pp, Data); if(diff&&dbp){
  3971. if(memcmp(dbp->Data, Data, sizeof(real)*len)){ la_GiveDiffCommand(diff, dbi, p, dbp->Data); dbp->Data=Data; ret=1; } else memFree(Data);
  3972. }
  3973. else laAddDBProp(dbi, p, Data);
  3974. return ret;
  3975. }
  3976. int la_AddStringDBProp(laDBInst* dbi, laDBProp* dbp, laDiff* diff, laPropPack *pp){
  3977. laProp *p = pp->LastPs->p; char _buf[LA_RAW_CSTR_MAX_LEN]={0}; char* buf=_buf; int ret=0;
  3978. laGetString(pp, _buf, &buf); if(diff&&dbp){
  3979. if((!dbp->Data&&buf[0]) || (buf[0] && strcmp(buf, ((laSafeString*)dbp->Data)->Ptr)) || (!buf[0]&&dbp->Data)){
  3980. laDiffCommand* dc=la_GiveDiffCommand(diff, dbi, p, dbp->Data); dbp->Data=0; strSafeSet(&dbp->Data, buf); ret=1; }
  3981. }else{
  3982. laDBProp* dbp=laAddDBProp(dbi, p, 0); strSafeSet(&dbp->Data, buf);
  3983. }
  3984. return ret;
  3985. }
  3986. int la_AddEnumDBProp(laDBInst* dbi, laDBProp* dbp, laDiff* diff, laPropPack *pp){
  3987. laProp *p = pp->LastPs->p; laEnumItem **Data=0; int len = laGetArrayLength(pp); int ret=0;
  3988. Data= memAcquireSimple(sizeof(laEnumItem*)*len);
  3989. laGetEnumArray(pp, Data); if(diff&&dbp){
  3990. if(memcmp(dbp->Data, Data, sizeof(laEnumItem*)*len)){ la_GiveDiffCommand(diff, dbi, p, dbp->Data); dbp->Data=Data; ret=1; } else memFree(Data);
  3991. }else laAddDBProp(dbi, p, Data);
  3992. return ret;
  3993. }
  3994. int la_AddRawDBProp(laDBInst* dbi, laDBRawProp* dbp, laDiff* diff, laPropPack *pp){
  3995. laProp *p = pp->LastPs->p; int *Data; int s=0; int ret=0; int IsCopy=0;
  3996. Data=laGetRaw(pp,&s,&IsCopy); if(diff&&dbp){
  3997. if(dbp->DataSize!=s || (!dbp->Data&&Data) || (!Data&&dbp->Data) || (dbp->Data&&Data&&memcmp(dbp->Data, Data, s))){
  3998. //printf("s%d %x %d \n",s,Data,dbp->DataSize);
  3999. void* NewData=(s&&Data)?calloc(1,s):0;
  4000. if(s&&Data)memcpy(NewData, Data, s);
  4001. laDiffCommandRaw* dcr=la_GiveDiffCommand(diff, dbi, p, dbp->Data);
  4002. dcr->DataSize=dbp->DataSize; dbp->Data=NewData; dbp->DataSize=s; ret=1;
  4003. }
  4004. }
  4005. else{
  4006. void* NewData=s?calloc(1,s):0; if(s) memcpy(NewData, Data, s);
  4007. laDBRawProp* dbp=laAddDBProp(dbi, p, NewData); dbp->DataSize=s;
  4008. }
  4009. if(IsCopy){ free(Data); }
  4010. return ret;
  4011. }
  4012. laDiffCommandInst* la_NewDiffCommandInst(laDBInst* DBInst, laDBInst* Prev, laDBInst* Next){
  4013. laDiffCommandInst* dci=memAcquire(sizeof(laDiffCommandInst));
  4014. dci->DBInst = DBInst; dci->OriginalPrev = Prev; dci->OriginalNext = Next;
  4015. dci->BeforePrev = DBInst->Item.pPrev; dci->BeforeNext = DBInst->Item.pNext;
  4016. return dci;
  4017. }
  4018. laDBInst* la_GetDiffDBInst(laListHandle* NewAdded, laListHandle* Master, void* instance){
  4019. if(!instance) return 0;
  4020. for(laDiffTemp* dt=NewAdded->pFirst;dt;dt=dt->Item.pNext){
  4021. if(((laDBInst*)dt->p)->OriginalInstance == instance) return dt->p;
  4022. }
  4023. for(laDBInst* dbi=Master->pFirst;dbi;dbi=dbi->Item.pNext){
  4024. if(dbi->OriginalInstance==instance) return dbi;
  4025. }
  4026. return 0;
  4027. }
  4028. int laIterateDB(laDBInst* parent, laPropPack* pp, laDiff* diff, laDBProp* dp);
  4029. int la_GenerateListDifferences(laDBInst* dbi, laDBSubProp* dbp, laPropPack* pp, laDiff* diff){
  4030. laProp *p = pp->LastPs->p, *subp = 0;
  4031. laPropIterator pi={0};
  4032. void* inst; int any=0;
  4033. laListHandle New={0}, NewAdded={0}, NewDeleted={0}, NewMoved={0};
  4034. inst = laGetInstance(p, pp->LastPs->UseInstance, &pi);
  4035. pp->EndInstance = inst;
  4036. while (inst){
  4037. if(p->UDFIsSingle){ memTake(inst); /* For single must take or it will be freed. */ }
  4038. lstAppendPointer(&New, inst);
  4039. inst = laGetNextInstance(p, inst, &pi);
  4040. pp->EndInstance = inst;
  4041. }
  4042. for(laDBInst* dbi=dbp->Instances.pFirst;dbi;dbi=dbi->Item.pNext){
  4043. void* OldPrev=dbi->Item.pPrev?((laDBInst*)dbi->Item.pPrev)->OriginalInstance:0;
  4044. void* OldNext=dbi->Item.pNext?((laDBInst*)dbi->Item.pNext)->OriginalInstance:0;
  4045. int found=0; for(laListItemPointer* lip=New.pFirst;lip;lip=lip->pNext){
  4046. void* NewPrev=lip->pPrev?((laListItemPointer*)lip->pPrev)->p:0;
  4047. void* NewNext=lip->pNext?((laListItemPointer*)lip->pNext)->p:0;
  4048. if(lip->p == dbi->OriginalInstance){
  4049. found=1;
  4050. if(OldPrev==NewPrev && OldNext==NewNext){break;}
  4051. else{ laDiffTemp* dt=lstAppendPointerSized(&NewMoved, dbi, sizeof(laDiffTemp));
  4052. dt->tPrev=dbi->Item.pPrev; dt->tNext=dbi->Item.pNext; dt->nPrev=NewPrev; dt->nNext=NewNext;break;}
  4053. }
  4054. }
  4055. if(!found){
  4056. laDiffTemp* dt=lstAppendPointerSized(&NewDeleted, dbi, sizeof(laDiffTemp));dt->tPrev=dbi->Item.pPrev;dt->tNext=dbi->Item.pNext; any++;
  4057. }
  4058. }
  4059. for(laListItemPointer* lip=New.pFirst;lip;lip=lip->pNext){
  4060. int found=0; for(laDBInst* dbi=dbp->Instances.pFirst;dbi;dbi=dbi->Item.pNext){
  4061. if(lip->p == dbi->OriginalInstance){ found=1; }
  4062. }
  4063. if(!found){ any++;
  4064. laDiffTemp* dt=lstAppendPointerSized(&NewAdded, lip->p, sizeof(laDiffTemp));
  4065. void* NewPrev=lip->pPrev?((laListItemPointer*)lip->pPrev)->p:0;
  4066. void* NewNext=lip->pNext?((laListItemPointer*)lip->pNext)->p:0;
  4067. dt->tPrev=NewPrev; dt->tNext=NewNext;
  4068. laPropContainer* spc=p->SubProp; if(((laSubProp*)p)->GetType){spc=((laSubProp*)p)->GetType(lip->p);}
  4069. laDBInst* newdbi=laAddDBInst(0, lip->p, spc, diff); dt->p=newdbi;
  4070. for (subp = spc->Props.pFirst; subp; subp = subp->Item.pNext){
  4071. if (subp->UDFIgnore || subp->UDFOnly) continue;
  4072. if (subp->PropertyType == LA_PROP_OPERATOR) continue;
  4073. laPropStep SubPS = {0}; laPropPack SubPP = {0}; laPropIterator pi={0};
  4074. SubPP.RawThis = pp; SubPS.p = subp; SubPS.UseInstance = lip->p; SubPP.LastPs=&SubPS;
  4075. any+=laIterateDB(newdbi, &SubPP, 0, 0);
  4076. }
  4077. }
  4078. }
  4079. for(laDiffTemp* lip=NewAdded.pFirst;lip;lip=lip->Item.pNext){
  4080. laDBInst* newdbi=lip->p;
  4081. lip->tPrev = la_GetDiffDBInst(&NewAdded, &dbp->Instances, lip->tPrev);
  4082. lip->tNext = la_GetDiffDBInst(&NewAdded, &dbp->Instances, lip->tNext);
  4083. }
  4084. for(laDiffTemp* lip=NewMoved.pFirst;lip;lip=lip->Item.pNext){
  4085. laDBInst* newdbi=lip->p;
  4086. lip->tPrev = la_GetDiffDBInst(&NewAdded, &dbp->Instances, lip->nPrev);
  4087. lip->tNext = la_GetDiffDBInst(&NewAdded, &dbp->Instances, lip->nNext);
  4088. }
  4089. laDiffCommandSub* dc=(NewAdded.pFirst||NewDeleted.pFirst||NewMoved.pFirst)?la_GiveDiffCommand(diff, dbi, p, 0):0;
  4090. for(laDiffTemp* lip=NewAdded.pFirst;lip;lip=lip->Item.pNext){
  4091. lstAppendItem(&dc->AddedInst, la_NewDiffCommandInst(lip->p, 0, 0));
  4092. laDBInst* newdbi=lip->p;
  4093. newdbi->Item.pPrev = lip->tPrev; newdbi->Item.pNext = lip->tNext;
  4094. if(!newdbi->Item.pPrev){dbp->Instances.pFirst=newdbi;}
  4095. if(!newdbi->Item.pNext){dbp->Instances.pLast=newdbi;}
  4096. }
  4097. for(laDiffTemp* lip=NewMoved.pFirst;lip;lip=lip->Item.pNext){
  4098. laDBInst* dbi=lip->p; lstAppendItem(&dc->MovedInst, la_NewDiffCommandInst(dbi, lip->tPrev, lip->tNext));
  4099. laDBInst* newdbi=lip->p;
  4100. newdbi->Item.pPrev = lip->tPrev; newdbi->Item.pNext = lip->tNext;
  4101. if(!dbi->Item.pPrev){dbp->Instances.pFirst=dbi;}
  4102. if(!dbi->Item.pNext){dbp->Instances.pLast=dbi;}
  4103. }
  4104. if(!New.pFirst){ dbp->Instances.pFirst=dbp->Instances.pLast=0; }
  4105. for(laDiffTemp* lip=NewDeleted.pFirst;lip;lip=lip->Item.pNext){printf("deleted %llx %llx\n", dbi, dbi->OriginalInstance);
  4106. laDBInst* dbi=lip->p; lstAppendItem(&dc->RemovedInst, la_NewDiffCommandInst(dbi, lip->tPrev, lip->tNext));
  4107. memTake(dbi->OriginalInstance);
  4108. //if(!dbi->Item.pPrev){dbp->Instances.pFirst=dbi->Item.pNext;}
  4109. //if(!dbi->Item.pNext){dbp->Instances.pLast=dbi->Item.pPrev;}
  4110. dbi->Item.pPrev=dbi->Item.pNext=0; //lstRemoveItem(&dbp->Instances, dbi);
  4111. laPropStep SubPS = {0}; laPropPack SubPP = {0}; laPropIterator pi={0}; laDBProp* dpi=0; SubPP.LastPs = &SubPS;
  4112. laPropContainer* spc=p->SubProp; if(((laSubProp*)p)->GetType){spc=((laSubProp*)p)->GetType(dbi->OriginalInstance);}
  4113. dpi=dbi->Props.pFirst;
  4114. for (subp = spc->Props.pFirst; subp; subp = subp->Item.pNext){
  4115. if (subp->PropertyType == LA_PROP_OPERATOR) continue;
  4116. if (subp->UDFIgnore || subp->UDFOnly) continue;
  4117. if((!dpi)||dpi->p!=subp) printf("Prop doesn't match\n");
  4118. SubPP.RawThis = pp; SubPS.p = subp; SubPS.UseInstance = dbi->OriginalInstance;
  4119. int thisany=laIterateDB(dbi, &SubPP, diff, dpi);
  4120. dpi=dpi->Item.pNext;
  4121. if(thisany){ any+=thisany; la_GiveExtraTouched(diff,dbi); }
  4122. }
  4123. dbi=dbi->Item.pNext;
  4124. }
  4125. //printf("%x ~ %x\n", dbp->Instances.pFirst, dbp->Instances.pLast);
  4126. //laListItem* item = laGetInstance(p, pp->LastPs->UseInstance, &pi);
  4127. //while (item){
  4128. // printf(" %x [%x] %x\n",item->pPrev, item, item->pNext);
  4129. // item = laGetNextInstance(p, item, &pi);
  4130. //}
  4131. return any;
  4132. }
  4133. int laIterateDB(laDBInst* parent, laPropPack* pp, laDiff* diff, laDBProp* dp){
  4134. laProp *p = pp->LastPs->p, *subp = 0;
  4135. laPropStep SubPS = {0}; laPropPack SubPP = {0}; laPropIterator pi={0};
  4136. laDBProp* dbp; laDBSubProp* dsp; laDBInst* dbi; laDBProp* dpi=0;
  4137. void *inst = 0;
  4138. SubPP.LastPs = &SubPS;
  4139. int any=0;
  4140. switch (p->PropertyType){
  4141. case LA_PROP_SUB:
  4142. if (p->UDFIsRefer){ if(!parent) return 0;
  4143. inst = laGetActiveInstanceStrict(p, pp->LastPs->UseInstance);
  4144. if(diff){
  4145. if(inst!=dp->Data) la_GiveDiffCommand(diff, parent, p, dp->Data); dp->Data=inst; any++;
  4146. }else{
  4147. laDBProp* rdbp=laAddDBProp(parent, p, inst);
  4148. }
  4149. }else{
  4150. if (!p->SubProp) p->SubProp = la_ContainerLookup(((laSubProp *)p)->TargetID);
  4151. if(diff){ any+=la_GenerateListDifferences(parent, dp, pp, diff); dsp=dp; dbi=dsp->Instances.pFirst; dbp=dsp; }
  4152. else{ dbp=laAddDBProp(parent, p, laGetActiveInstance(p, pp->LastPs->UseInstance, &pi)); }
  4153. inst = laGetInstance(p, pp->LastPs->UseInstance, &pi);
  4154. pp->EndInstance = inst;
  4155. while (inst){ //printf("work %x\n",inst);
  4156. laPropContainer* spc=p->SubProp; if(((laSubProp*)p)->GetType){spc=((laSubProp*)p)->GetType(inst);}
  4157. if(!spc){ inst = laGetNextInstance(p, inst, &pi); continue; } // unlikely, because not registered
  4158. if(diff){
  4159. if(dbi->OriginalInstance != inst){
  4160. printf("dbi/inst doesn't match.\n"); }
  4161. dpi=dbi->Props.pFirst;
  4162. }else{ dbi = laAddDBInst(dbp, inst, spc, 0); }
  4163. for (subp = spc->Props.pFirst; subp; subp = subp->Item.pNext){
  4164. if (subp->PropertyType == LA_PROP_OPERATOR) continue;
  4165. if (subp->UDFIgnore || subp->UDFOnly) continue;
  4166. if(diff && ((!dpi)||dpi->p!=subp)){
  4167. printf("Prop doesn't match\n");}
  4168. SubPP.RawThis = pp; SubPS.p = subp; SubPS.UseInstance = inst;
  4169. int thisany=laIterateDB(dbi, &SubPP, diff, dpi);
  4170. if(diff && dpi){ dpi=dpi->Item.pNext;
  4171. if(thisany){ any+=thisany;
  4172. la_GiveExtraTouched(diff,dbi); }
  4173. }
  4174. }
  4175. inst = laGetNextInstance(p, inst, &pi);
  4176. pp->EndInstance = inst;
  4177. if(diff){ dbi=dbi->Item.pNext; }
  4178. }
  4179. }
  4180. break;
  4181. case LA_PROP_INT: case LA_PROP_ARRAY | LA_PROP_INT:
  4182. any+=la_AddIntDBProp(parent, dp, diff, pp); break;
  4183. case LA_PROP_FLOAT: case LA_PROP_ARRAY | LA_PROP_FLOAT:
  4184. any+=la_AddFloatDBProp(parent, dp, diff, pp); break;
  4185. case LA_PROP_STRING:
  4186. any+=la_AddStringDBProp(parent, dp, diff, pp); break;
  4187. case LA_PROP_ENUM: case LA_PROP_ARRAY | LA_PROP_ENUM:
  4188. any+=la_AddEnumDBProp(parent, dp, diff, pp); break;
  4189. case LA_PROP_RAW:
  4190. any+=la_AddRawDBProp(parent,dp,diff,pp); break;
  4191. default: break;
  4192. }
  4193. return any;
  4194. }
  4195. void la_RestoreIntDBProp(laDBProp* dbp, laDiffCommand* dc){
  4196. laPropPack pp={0}; laPropStep ps={0}; ps.p=dc->p; pp.LastPs=&ps; ps.UseInstance=dc->Instance->OriginalInstance;
  4197. int *Data; Data=dc->Data; dc->Data=dbp->Data; dbp->Data=Data;
  4198. laSetIntArrayAllArray(&pp, dbp->Data);
  4199. }
  4200. void la_RestoreFloatDBProp(laDBProp* dbp, laDiffCommand* dc){
  4201. laPropPack pp={0}; laPropStep ps={0}; ps.p=dc->p; pp.LastPs=&ps; ps.UseInstance=dc->Instance->OriginalInstance;
  4202. int *Data; Data=dc->Data; dc->Data=dbp->Data; dbp->Data=Data;
  4203. laSetFloatArrayAllArray(&pp, dbp->Data);
  4204. }
  4205. void la_RestoreStringDBProp(laDBProp* dbp, laDiffCommand* dc){
  4206. laPropPack pp={0}; laPropStep ps={0}; ps.p=dc->p; pp.LastPs=&ps; ps.UseInstance=dc->Instance->OriginalInstance;
  4207. int *Data; Data=dc->Data; dc->Data=dbp->Data; dbp->Data=Data;
  4208. laSetString(&pp, dbp->Data?((laSafeString*)dbp->Data)->Ptr:0);
  4209. }
  4210. void la_RestoreEnumDBProp(laDBProp* dbp, laDiffCommand* dc){
  4211. laPropPack pp={0}; laPropStep ps={0}; ps.p=dc->p; pp.LastPs=&ps; ps.UseInstance=dc->Instance->OriginalInstance;
  4212. int *Data; Data=dc->Data; dc->Data=dbp->Data; dbp->Data=Data;
  4213. laSetEnumArrayAllArray(&pp, dbp->Data);
  4214. }
  4215. void la_RestoreRawDBProp(laDBRawProp* dbp, laDiffCommandRaw* dc){
  4216. laPropPack pp={0}; laPropStep ps={0}; ps.p=dc->p; pp.LastPs=&ps; ps.UseInstance=dc->Instance->OriginalInstance;
  4217. int *Data; Data=dc->Data; dc->Data=dbp->Data; dbp->Data=Data; LA_SWAP(int, dc->DataSize, dbp->DataSize);
  4218. laSetRaw(&pp,dbp->Data,dbp->DataSize);
  4219. }
  4220. void la_AddUndoPostNode(laDBInst* dbi){
  4221. if((!dbi->pc) || (!dbi->pc->UndoTouched)) return;
  4222. for(laDiffPost* dp=MAIN.DiffTouched.pLast;dp;dp=dp->Item.pPrev){ if(dp->instance==dbi->OriginalInstance&&dp->Touched==dbi->pc->UndoTouched) return; }
  4223. laDiffPost* dp=lstAppendPointerSized(&MAIN.DiffTouched, dbi->OriginalInstance, sizeof(laDiffPost));
  4224. dp->Touched=dbi->pc->UndoTouched;
  4225. }
  4226. void la_ExecUndoPtrSync(laDiff* d){
  4227. int hint=d->Hint; laDiffPtrSync* dps; laDiffPost* dp; laDBInstPendingRelink* dpr;
  4228. while(dp=lstPopItem(&MAIN.DiffTouched)){
  4229. dp->Touched(dp->instance, hint);
  4230. memFree(dp);
  4231. }
  4232. for(laDiffExtraTouched*det=d->ExtraTouched.pFirst;det;det=det->Item.pNext){
  4233. det->dbi->pc->UndoTouched(det->dbi->OriginalInstance, hint);
  4234. }
  4235. }
  4236. void la_FreeInstance(void* inst, laPropContainer* pc, int no_free){
  4237. //if(p->PropertyType!=LA_PROP_SUB) return;
  4238. //if(!p->SubProp || ((laSubProp*)p)->TargetID) p->SubProp=la_ContainerLookup(((laSubProp*)p)->TargetID);
  4239. //laPropContainer* pc=p->SubProp; if(((laSubProp*)p)->GetType) pc=((laSubProp*)p)->GetType(inst);
  4240. //printf("freeinst %s %x\n",pc->Name,inst);
  4241. if(pc->BeforeFree) pc->BeforeFree(inst);
  4242. laPropStep SubPS = {0}; laPropPack SubPP = {0}; laPropIterator pi={0}; SubPP.LastPs=&SubPS;
  4243. for(laProp* p=pc->Props.pFirst;p;p=p->Item.pNext){
  4244. if(p->PropertyType==LA_PROP_STRING && ((laStringProp*)p)->IsSafeString){ SubPS.p=p; SubPS.UseInstance=inst; laSetString(&SubPP, 0); continue; }
  4245. //if(p->PropertyType!=LA_PROP_SUB || p->UDFIsRefer) continue;
  4246. //void* si = laGetInstance(p, SubPP.LastPs->UseInstance, &pi); SubPP.EndInstance = si; void* NextSi=0;
  4247. //printf("freeinst p %s\n",p->Name);
  4248. //while (si){
  4249. // printf("%x --inst %x\n",SubPP.LastPs->UseInstance,si);
  4250. // NextSi = laGetNextInstance(p, si, &pi);
  4251. // if(!p->UDFNoCreate){ laSubProp* sp=p;
  4252. // if(!sp->ListHandleOffset){ logPrint("[WARN] prop '%s' UDFNoCreate==0 and no ListHandleOffset. Node not removed.\n", p->Identifier); }
  4253. // else{ lstRemoveItem(inst + sp->ListHandleOffset, si); }
  4254. // }
  4255. // la_FreeInstance(si, p, p->UDFNoCreate||(!p->OffsetIsPointer));
  4256. // si=NextSi; SubPP.EndInstance = si;
  4257. //}
  4258. }
  4259. if(!no_free && !pc->OtherAlloc) memFree(inst);
  4260. }
  4261. void la_ResetInstance(void* inst, laPropContainer* pc, int IsListItem){
  4262. if(pc->SaverDummy) laMarkMemClean(((char*)inst)+((laSubProp*)pc->SaverDummy)->ListHandleOffset);
  4263. if(pc->Reset) pc->Reset(inst); //else { int a=IsListItem?sizeof(laListItem):0; memset(((char*)inst)+a,0,pc->NodeSize-a); }
  4264. }
  4265. laListHandle* la_GetOriginalListHandle(laDiffCommandSub* dcs){
  4266. laProp*p =dcs->Base.p;
  4267. if(p->PropertyType!=LA_PROP_SUB||p->UDFIsRefer||!((laSubProp*)p)->ListHandleOffset) return 0;
  4268. laSubProp* sp=dcs->Base.p;
  4269. void* addr=((char*)dcs->Base.Instance->OriginalInstance)+((laSubProp*)p)->ListHandleOffset;
  4270. return addr;
  4271. }
  4272. void la_UndoListDifferences(laDBSubProp* dsp, laDiffCommandSub* dcs){
  4273. laListHandle* ol=la_GetOriginalListHandle(dcs);
  4274. for(laDiffCommandInst* dci=dcs->AddedInst.pFirst;dci;dci=dci->Item.pNext){
  4275. if(dcs->Base.p->UDFNoCreate){ la_ResetInstance(dci->DBInst->OriginalInstance,dci->DBInst->pc,((laSubProp*)dcs->Base.p)->ListHandleOffset?1:0); continue; }
  4276. dci->OriginalPrev = dci->DBInst->Item.pPrev; dci->OriginalNext = dci->DBInst->Item.pNext;
  4277. //if(dsp->Instances.pFirst == dci->DBInst){ dsp->Instances.pFirst=dci->DBInst->Item.pNext; ol->pFirst=dci->DBInst->Item.pNext?((laDBInst*)dci->DBInst->Item.pNext)->OriginalInstance:0; }
  4278. //if(dsp->Instances.pLast == dci->DBInst){ dsp->Instances.pLast=dci->DBInst->Item.pPrev; ol->pLast=dci->DBInst->Item.pPrev?((laDBInst*)dci->DBInst->Item.pPrev)->OriginalInstance:0; }
  4279. //dci->DBInst->Item.pPrev = dci->DBInst->Item.pNext=0;
  4280. }
  4281. for(laDiffCommandInst* dci=dcs->AddedInst.pFirst;dci;dci=dci->Item.pNext){
  4282. if(dcs->Base.p->UDFNoCreate){ continue; }
  4283. lstRemoveItem(&dsp->Instances, dci->DBInst); lstRemoveItem(ol, dci->DBInst->OriginalInstance);
  4284. laListItem* li = dci->DBInst->OriginalInstance;
  4285. }
  4286. for(laDiffCommandInst* dci=dcs->RemovedInst.pFirst;dci;dci=dci->Item.pNext){ if(dcs->Base.p->UDFNoCreate) continue;
  4287. dci->DBInst->Item.pPrev = dci->OriginalPrev;
  4288. dci->DBInst->Item.pNext = dci->OriginalNext; printf("add removed %s %llx \n",dci->DBInst->pc->Identifier, dci->DBInst->OriginalInstance);
  4289. void* orig = dci->DBInst->OriginalInstance;
  4290. }
  4291. for(laDiffCommandInst* dci=dcs->RemovedInst.pFirst;dci;dci=dci->Item.pNext){ if(dcs->Base.p->UDFNoCreate) continue;
  4292. ((laListItem*)dci->DBInst->OriginalInstance)->pPrev=dci->OriginalPrev?dci->OriginalPrev->OriginalInstance:0;
  4293. ((laListItem*)dci->DBInst->OriginalInstance)->pNext=dci->OriginalNext?dci->OriginalNext->OriginalInstance:0;
  4294. if(!dci->DBInst->Item.pPrev){ dsp->Instances.pFirst=dci->DBInst; ol->pFirst=dci->DBInst->OriginalInstance; }
  4295. if(!dci->DBInst->Item.pNext){ dsp->Instances.pLast=dci->DBInst; ol->pLast=dci->DBInst->OriginalInstance; }
  4296. }
  4297. for(laDiffCommandInst* dci=dcs->MovedInst.pFirst;dci;dci=dci->Item.pNext){
  4298. dci->DBInst->Item.pPrev = dci->BeforePrev; ((laListItem*)dci->DBInst->OriginalInstance)->pPrev=dci->BeforePrev?dci->BeforePrev->OriginalInstance:0;
  4299. dci->DBInst->Item.pNext = dci->BeforeNext; ((laListItem*)dci->DBInst->OriginalInstance)->pNext=dci->BeforeNext?dci->BeforeNext->OriginalInstance:0;
  4300. if(!dci->DBInst->Item.pPrev){ dsp->Instances.pFirst=dci->DBInst; ol->pFirst=dci->DBInst->OriginalInstance; }
  4301. if(!dci->DBInst->Item.pNext){ dsp->Instances.pLast=dci->DBInst; ol->pLast=dci->DBInst->OriginalInstance; }
  4302. }
  4303. }
  4304. void la_RedoListDifferences(laDBSubProp* dsp, laDiffCommandSub* dcs){
  4305. laListHandle* ol=la_GetOriginalListHandle(dcs);
  4306. for(laDiffCommandInst* dci=dcs->RemovedInst.pFirst;dci;dci=dci->Item.pNext){
  4307. if(dcs->Base.p->UDFNoCreate){ la_ResetInstance(dci->DBInst->OriginalInstance,dci->DBInst->pc,((laSubProp*)dcs->Base.p)->ListHandleOffset?1:0); continue; }
  4308. //dci->OriginalPrev = dci->DBInst->Item.pPrev;
  4309. //dci->OriginalNext = dci->DBInst->Item.pNext;
  4310. lstRemoveItem(&dsp->Instances, dci->DBInst); lstRemoveItem(ol, dci->DBInst->OriginalInstance);
  4311. laListItem* li = dci->DBInst->OriginalInstance;
  4312. printf("remove %llx \n", dci->DBInst->OriginalInstance);
  4313. }
  4314. for(laDiffCommandInst* dci=dcs->AddedInst.pFirst;dci;dci=dci->Item.pNext){ if(dcs->Base.p->UDFNoCreate) continue;
  4315. dci->DBInst->Item.pPrev = dci->OriginalPrev;
  4316. dci->DBInst->Item.pNext = dci->OriginalNext;
  4317. void* orig = dci->DBInst->OriginalInstance;
  4318. }
  4319. for(laDiffCommandInst* dci=dcs->AddedInst.pFirst;dci;dci=dci->Item.pNext){ if(dcs->Base.p->UDFNoCreate) continue;
  4320. ((laListItem*)dci->DBInst->OriginalInstance)->pPrev=dci->OriginalPrev?dci->OriginalPrev->OriginalInstance:0;
  4321. ((laListItem*)dci->DBInst->OriginalInstance)->pNext=dci->OriginalNext?dci->OriginalNext->OriginalInstance:0;
  4322. if(!dci->DBInst->Item.pPrev){ dsp->Instances.pFirst=dci->DBInst; ol->pFirst=dci->DBInst->OriginalInstance; }
  4323. if(!dci->DBInst->Item.pNext){ dsp->Instances.pLast=dci->DBInst; ol->pLast=dci->DBInst->OriginalInstance; }
  4324. }
  4325. for(laDiffCommandInst* dci=dcs->MovedInst.pFirst;dci;dci=dci->Item.pNext){
  4326. dci->DBInst->Item.pPrev = dci->OriginalPrev; ((laListItem*)dci->DBInst->OriginalInstance)->pPrev=dci->OriginalPrev?dci->OriginalPrev->OriginalInstance:0;
  4327. dci->DBInst->Item.pNext = dci->OriginalNext; ((laListItem*)dci->DBInst->OriginalInstance)->pNext=dci->OriginalNext?dci->OriginalNext->OriginalInstance:0;
  4328. if(!dci->DBInst->Item.pPrev){ dsp->Instances.pFirst=dci->DBInst; ol->pFirst=dci->DBInst->OriginalInstance; }
  4329. if(!dci->DBInst->Item.pNext){ dsp->Instances.pLast=dci->DBInst; ol->pLast=dci->DBInst->OriginalInstance; }
  4330. }
  4331. }
  4332. laDBProp* la_FindDBProp(laDBInst* dbi, laProp* p){
  4333. for(laDBProp* dbp=dbi->Props.pFirst;dbp;dbp=dbp->Item.pNext){
  4334. if(dbp->p==p) return dbp;
  4335. }
  4336. return 0;
  4337. }
  4338. void tnsPrintMaterials();
  4339. laDiff* laSwapDBState(int Redo){
  4340. laDiff* diff=MAIN.HeadDifference; if(!diff) return 0;
  4341. if(Redo){ if(diff==MAIN.Differences.pLast) return 0; }else{ diff=diff->Item.pPrev; if(!diff) return 0; }
  4342. for(laDiffCommand* dc=diff->Commands.pFirst;dc;dc=dc->Item.pNext){
  4343. //printf("do %s\n",dc->p->Identifier);
  4344. laDBProp* dbp=la_FindDBProp(dc->Instance, dc->p);
  4345. if(!dbp){ printf("Can't find dbp from prop!\n");}
  4346. switch (dc->p->PropertyType){
  4347. case LA_PROP_SUB:
  4348. if(dc->p->UDFIsRefer){
  4349. LA_SWAP(void*,dc->Data,dbp->Data);
  4350. laSetActiveInstance(dbp->p, dc->Instance->OriginalInstance, dbp->Data);
  4351. }else{
  4352. if(Redo) la_RedoListDifferences(dbp,dc);
  4353. else la_UndoListDifferences(dbp,dc);
  4354. }
  4355. la_AddUndoPostNode(dc->Instance);
  4356. break;
  4357. case LA_PROP_INT: case LA_PROP_ARRAY | LA_PROP_INT:
  4358. la_RestoreIntDBProp(dbp, dc); la_AddUndoPostNode(dc->Instance); break;
  4359. case LA_PROP_FLOAT: case LA_PROP_ARRAY | LA_PROP_FLOAT:
  4360. la_RestoreFloatDBProp(dbp, dc); la_AddUndoPostNode(dc->Instance); break;
  4361. case LA_PROP_STRING:
  4362. la_RestoreStringDBProp(dbp, dc); la_AddUndoPostNode(dc->Instance); break;
  4363. case LA_PROP_ENUM: case LA_PROP_ARRAY | LA_PROP_ENUM:
  4364. la_RestoreEnumDBProp(dbp, dc); la_AddUndoPostNode(dc->Instance); break;
  4365. case LA_PROP_RAW:
  4366. la_RestoreRawDBProp(dbp, dc); la_AddUndoPostNode(dc->Instance); break;
  4367. default: break;
  4368. }
  4369. laPropPack PP={0}; laPropStep PS={0}; PP.LastPs=&PS; PS.p=dbp->p; PS.UseInstance=dc->Instance->OriginalInstance;
  4370. laNotifyUsersPP(&PP);
  4371. }
  4372. for(laDiffCommandCustom* dcc=diff->CustomCommands.pFirst;dcc;dcc=dcc->Item.pNext){
  4373. if(Redo){ if(dcc->Redo) dcc->Redo(dcc->Data); } else { if(dcc->Undo) dcc->Undo(dcc->Data); }
  4374. }
  4375. tnsPrintMaterials();
  4376. if(Redo){ MAIN.HeadDifference=diff->Item.pNext; }
  4377. else{ MAIN.HeadDifference=diff; }
  4378. return diff;
  4379. }
  4380. void laUndo(){ laDiff* d; if(d=laSwapDBState(0)){ la_ExecUndoPtrSync(d); laNotifyUsers("la.differences"); } }
  4381. void laRedo(){ laDiff* d; if(d=laSwapDBState(1)){ la_ExecUndoPtrSync(d); laNotifyUsers("la.differences"); } }
  4382. void laPrintDBInst(laDBInst* dbi, int Level){
  4383. if(dbi!=&MAIN.RootDBInst){
  4384. logPrintNew("%*c", Level, ' ');
  4385. logPrint("%s | %.6x [%.6x] %.6x | %.6x [%.6x] %.6x\n",
  4386. dbi->pc->Identifier, dbi->Item.pPrev, dbi, dbi->Item.pNext,
  4387. dbi->Item.pPrev?((laDBInst*)dbi->Item.pPrev)->OriginalInstance:0, dbi->OriginalInstance, dbi->Item.pNext?((laDBInst*)dbi->Item.pNext)->OriginalInstance:0);
  4388. }else{
  4389. logPrintNew("Root:\n");
  4390. }
  4391. for(laDBProp* dbp=dbi->Props.pFirst;dbp;dbp=dbp->Item.pNext){
  4392. if(dbp->p->PropertyType==LA_PROP_SUB && !dbp->p->UDFIsRefer){
  4393. laDBSubProp* dsp=dbp;
  4394. for(laDBInst* dbii=dsp->Instances.pFirst;dbii;dbii=dbii->Item.pNext){
  4395. laPrintDBInst(dbii, Level+4);
  4396. }
  4397. }
  4398. }
  4399. }
  4400. void laPrintDBInstInfo(){
  4401. laPrintDBInst(&MAIN.RootDBInst, 0);
  4402. }
  4403. void laAddRootDBInst(char* path){
  4404. laPropPack PP={0};
  4405. if(!la_GetPropFromPath(&PP,0,path,0)) return;
  4406. if(!MAIN.DBInstLink){ hsh65536Init(&MAIN.DBInstLink); }
  4407. la_StepPropPack(&PP);
  4408. laIterateDB(&MAIN.RootDBInst, &PP, 0, 0);
  4409. la_FreePropStepCache(PP.Go);
  4410. laDBRecordedProp* rp=lstAppendPointerSized(&MAIN.DBRecordedProps, 0, sizeof(laDBRecordedProp));
  4411. strSafeSet(&rp->OriginalPath,path);
  4412. }
  4413. void laPropPackToLocal(laPropPack* ToPP, laPropPack* pp){
  4414. if(pp->RawThis) laPropPackToLocal(ToPP, pp->RawThis);
  4415. for(laPropStep* ps=pp->Go;ps;ps=ps->pNext){
  4416. la_NewPropStep(ToPP, ps->p, ps->UseInstance, '.');
  4417. }
  4418. ToPP->EndInstance = pp->EndInstance;
  4419. }
  4420. laDBProp* laFindDBProp(laDBInst* parent, laProp* p, void* Instance, laDBInst** r_DBInst){
  4421. laDBProp* rp=0;
  4422. for(laDBProp* dp=parent->Props.pFirst;dp;dp=dp->Item.pNext){
  4423. if(dp->p == p) {rp=dp; break;}
  4424. } if(!rp) return 0;
  4425. if(rp->p->PropertyType==LA_PROP_SUB && !rp->p->UDFIsRefer && r_DBInst && Instance){
  4426. laDBSubProp* dsp=rp; for(laDBInst* dbii=dsp->Instances.pFirst;dbii;dbii=dbii->Item.pNext){
  4427. if(dbii->OriginalInstance == Instance) { *r_DBInst=dbii; break; }
  4428. }
  4429. }
  4430. return rp;
  4431. }
  4432. laDBProp* laFindStartingDBProp(laProp* p, void* Instance, laPropContainer* InstancePC, laDBInst** r_DBInst){
  4433. if(!Instance||Instance==MAIN.DataRoot.RootInstance){
  4434. for(laDBProp*dbp=MAIN.RootDBInst.Props.pFirst;dbp;dbp=dbp->Item.pNext){
  4435. if(dbp->p == p){ *r_DBInst=&MAIN.RootDBInst; return dbp; }
  4436. }
  4437. } /* If root not found try to find it in the hash. */
  4438. if(!MAIN.DBInstLink){ return 0; }
  4439. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstLink, Instance); if(!l) return 0;
  4440. for(laListItem* li=l->pFirst;li;li=li->pNext){
  4441. laDBInst* dbi=(laDBInst*)(((char*)li)-sizeof(laListItem)); if(dbi->OriginalInstance==Instance && dbi->pc==InstancePC){
  4442. if(p){ for(laDBProp*dbp=dbi->Props.pFirst;dbp;dbp=dbp->Item.pNext){ if(dbp->p == p){ *r_DBInst=dbi; return dbp; } } }
  4443. else{ *r_DBInst=dbi; return 0; }
  4444. }
  4445. }
  4446. return 0;
  4447. }
  4448. void laRecordCustomDifferences(void* Data, laDiffCommandUndoF Undo, laDiffCommandRedoF Redo, laDiffCommandFreeF Free){
  4449. laDiffCommandCustom* dcc=memAcquire(sizeof(laDiffCommandCustom));
  4450. dcc->Data=Data; dcc->Undo=Undo; dcc->Redo=Redo; dcc->Free=Free;
  4451. lstAppendItem(&MAIN.HeadDifference->CustomCommands,dcc);
  4452. }
  4453. int laRecordDifferences(laPropPack* base, char* path){
  4454. laPropPack PP={0};
  4455. if(!la_GetPropFromPath(&PP,base,path,0)) return 0;
  4456. la_StepPropPack(&PP);
  4457. //laPropPackToLocal(&LocalPP, &PP);
  4458. int success=0;
  4459. laDBInst* FromDBI; //=laSkipDB(&LocalPP, &MAIN.RootDBInst, &dbp);
  4460. laDBProp* dbp=laFindStartingDBProp(PP.LastPs->p, PP.LastPs->UseInstance, PP.LastPs->p->Container, &FromDBI);
  4461. if(FromDBI && dbp){
  4462. laFreeNewerDifferences();
  4463. laIterateDB(FromDBI, &PP, MAIN.HeadDifference, dbp);
  4464. if(MAIN.HeadDifference->Commands.pFirst){ success = 1; }
  4465. }else{
  4466. success = 0; //printf("Prop not recorded as DBInst.\n");
  4467. }
  4468. la_FreePropStepCache(PP.Go);
  4469. return success;
  4470. }
  4471. int laRecordInstanceDifferences(void* instance, const char* container){
  4472. laPropContainer* pc=la_ContainerLookup(container); if(!pc) return 0;
  4473. laDBInst* FromDBI=0;
  4474. laFindStartingDBProp(0,instance,pc,&FromDBI); if(!FromDBI) return 0;
  4475. laPropPack PP={0}; laPropStep PS={0}; PS.UseInstance=instance; PP.LastPs=&PS;
  4476. int freed=0, success=0, any=0;
  4477. for(laProp* p=pc->Props.pFirst;p;p=p->Item.pNext){
  4478. PS.p = p; laDBProp* dbp=laFindDBProp(FromDBI, p, 0,0);
  4479. if(FromDBI && dbp){
  4480. if(!freed){ laFreeNewerDifferences(); freed=1; }
  4481. any+=laIterateDB(FromDBI, &PP, MAIN.HeadDifference, dbp);
  4482. if(MAIN.HeadDifference->Commands.pFirst){ success = 1; }
  4483. }
  4484. }
  4485. if(any){ la_GiveExtraTouched(MAIN.HeadDifference,FromDBI); }
  4486. return success;
  4487. }
  4488. void laRecordAndPush(laPropPack* base, char* path, char* description, uint64_t hint){
  4489. if(laRecordDifferences(base, path)){ laPushDifferences(description, hint); }
  4490. }
  4491. void laRecordAndPushProp(laPropPack* base, char* path){
  4492. char buf[256]={0};
  4493. if(laRecordDifferences(base, path)){
  4494. la_GetPropPackFullPath(base,&buf[strlen(buf)]);if(path){sprintf(&buf[strlen(buf)],"%s%s",base?".":"",path);}
  4495. laPushDifferences(buf, 0);
  4496. }
  4497. }
  4498. void laRecordEverythingAndPush(){
  4499. for(laDBRecordedProp* rp=MAIN.DBRecordedProps.pFirst;rp;rp=rp->Item.pNext){
  4500. laRecordAndPushProp(0, rp->OriginalPath->Ptr);
  4501. }
  4502. if(MAIN.PushEverything){ MAIN.PushEverything(); }
  4503. }
  4504. void laSetDiffCallback(laDiffPushEverythingF PushEverything){
  4505. MAIN.PushEverything=PushEverything;
  4506. }