1+ '''
2+ Copyright (c) 2025 Pyogenics <https://github.com/Pyogenics>
3+
4+ Permission is hereby granted, free of charge, to any person obtaining a copy
5+ of this software and associated documentation files (the "Software"), to deal
6+ in the Software without restriction, including without limitation the rights
7+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ copies of the Software, and to permit persons to whom the Software is
9+ furnished to do so, subject to the following conditions:
10+
11+ The above copyright notice and this permission notice shall be included in all
12+ copies or substantial portions of the Software.
13+
14+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ SOFTWARE.
21+ '''
22+
23+ from .IOTools import unpackStream
24+ from . import AlternativaProtocol
25+
26+ class LightmapData :
27+ def __init__ (self ):
28+ self .lightColour = (0.0 , 0.0 , 0.0 )
29+ self .ambientLightColour = (0.0 , 0.0 , 0.0 )
30+ self .lightAngle = (0.0 , 0.0 ) # (x, z)
31+ self .lightmaps = []
32+ self .mapObjects = []
33+
34+ def read (self , stream ):
35+ print ("Reading LightmapData" )
36+
37+ # There is no signature so just start reading data and hope this is actually a lightmap data file
38+ version , = unpackStream ("<I" , stream )
39+ print (f"Reading LightmapData version { version } " )
40+
41+ if version == 1 :
42+ self .read1 (stream )
43+ elif version == 2 :
44+ self .read2 (stream )
45+ else :
46+ raise RuntimeError (f"Unknown LightmapData version: { version } " )
47+
48+ '''
49+ Version specific readers
50+ '''
51+ def read1 (self , stream ):
52+ raise RuntimeError ("Version 1 LightmapData is not implemented yet" )
53+
54+ def read2 (self , stream ):
55+ # Light info
56+ self .lightColour , self .ambientLightColour = unpackStream ("<2I" , stream )
57+ self .lightAngle = unpackStream ("<2f" , stream )
58+
59+ # Lightmaps
60+ lightmapCount , = unpackStream ("<I" , stream )
61+ print (f"Reading { lightmapCount } lightmaps" )
62+ for _ in range (lightmapCount ):
63+ lightmap = AlternativaProtocol .readString (stream )
64+ self .lightmaps .append (lightmap )
65+
66+ # Map objects
67+ mapObjectCount , = unpackStream ("<I" , stream )
68+ print (f"Reading { mapObjectCount } map objects" )
69+ for _ in range (mapObjectCount ):
70+ mapObject = MapObject ()
71+ mapObject .read (stream )
72+ self .mapObjects .append (mapObject )
73+
74+ #XXX: there is more data but do we actually care about it?
75+
76+ print (f"[LightmapData2 lightColour: { hex (self .lightColour )} ambientLightColour: { hex (self .ambientLightColour )} lightAngle: { self .lightAngle } ]" )
77+
78+ '''
79+ Objects
80+ '''
81+ class MapObject :
82+ def __init__ (self ):
83+ self .index = 0
84+ self .lightmapIndex = 0
85+ self .lightmapScaleOffset = (0.0 , 0.0 , 0.0 , 0.0 )
86+ self .UV1 = []
87+ self .UV2 = []
88+ self .castShadows = False
89+ self .recieveShadows = False
90+
91+ def read (self , stream ):
92+ self .index , self .lightmapIndex = unpackStream ("<2i" , stream )
93+
94+ # Read lightmap data
95+ if self .lightmapIndex >= 0 :
96+ self .lightmapScaleOffset = unpackStream ("<4f" , stream )
97+
98+ # Check if we have UVs and read them
99+ hasUVs , = unpackStream ("b" , stream )
100+ if hasUVs > 0 :
101+ vertexCount , = unpackStream ("<I" , stream )
102+ for _ in range (vertexCount // 2 ):
103+ UV1 = unpackStream ("<2f" , stream )
104+ self .UV1 .append (UV1 )
105+ UV2 = unpackStream ("<2f" , stream )
106+ self .UV2 .append (UV2 )
107+
108+ # Light settings
109+ castShadows , recieveShadows = unpackStream ("2b" , stream )
110+ self .castShadows = castShadows > 0
111+ self .recieveShadows = recieveShadows > 0
112+
113+ print (f"[MapObject index: { self .index } lightmapIndex: { self .lightmapIndex } lightmapScaleOffset: { self .lightmapScaleOffset } UV1: { len (self .UV1 )} UV2: { len (self .UV2 )} castShadows: { self .castShadows } recieveShadows: { self .recieveShadows } ]" )
0 commit comments