-
Notifications
You must be signed in to change notification settings - Fork 0
/
Npc.hs
583 lines (528 loc) · 20.1 KB
/
Npc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
{-# OPTIONS -Wall -fno-warn-name-shadowing #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad
import Control.Monad.Writer hiding (All)
import Data.Char
import Data.List
import Data.Map (Map)
import qualified Data.Map as M
import Data.Maybe (mapMaybe)
import Data.Set (Set)
import qualified Data.Set as S
import System.Console.GetOpt
import System.Directory
import System.Environment (getArgs)
import System.Exit
import System.FilePath.Posix
import PacketParser
type CppWriter = Writer String ()
{---------- Utility Functions ----------}
tellLn :: MonadWriter String m => String -> m ()
tellLn w = do
tell w
tell "\n"
lnBreak :: MonadWriter String m => m ()
lnBreak = tell "\n"
-- | Generates the comment given the file name
autoComment :: String -> String
autoComment f = "/* *** " ++ f ++ ": Auto-generated using npc *** */\n"
-- | Capitalizes a word
capitalize :: String -> String
capitalize "" = ""
capitalize (c:cs) = toUpper c : cs
-- | Determines what files need to be included for a type
has :: Map String FilePath -> CType -> Set String
has _ CString = S.singleton "<string>"
has m (Pair t1 t2) = S.insert "<utility>" (S.union (has m t1) (has m t2))
has m (Vector t) = S.insert "<vector>" (has m t)
has m (List t) = S.insert "<list>" (has m t)
has m (Map k v) = S.insert "<map>" (S.union (has m k) (has m v))
has m (User t) = S.singleton ('\"' : m M.! t ++ "\"")
has _ (Prim "time_t") = S.singleton "<ctime>"
has _ (Prim "size_t") = S.singleton "<cstdlib>"
has _ (Prim _) = S.empty
-- | Determines what files need to be included for a set of packets
includes :: Map String FilePath -> [Packet] -> Set String
includes m = foldl (\inc (Packet _ fs) -> S.union inc $
(foldl (\inc2 (Field t _) ->
S.union inc2 (has m t)) S.empty fs)) S.empty
-- | Takes the name of a class and converts it to CONSTANT_CASE
constantCase :: String -> String
constantCase "" = ""
constantCase (c:cs) = toUpper c : constantCase' cs
where
constantCase' "" = ""
constantCase' (c:cs)
| isUpper c = '_' : c : constantCase' cs
| otherwise = toUpper c : constantCase' cs
-- | Gets the type string for a const-correct type
ccType :: CType -> String
ccType (Prim t) = t ++ " "
ccType t = "const " ++ show t ++ " &"
{---------- Class Definitions (.h) ----------}
-- | Writes the header inclusions
writeIncludes :: Map String FilePath -> [Packet] -> CppWriter
writeIncludes m ps =
forM_ (S.toList (includes m ps)) (\f -> tellLn ("#include " ++ f))
-- | Writes a field's getter, optionally making it const
getter :: Field -> Bool -> CppWriter
getter (Field t n) const = do
tell " "
tell (if const then ccType t else show t ++ " &")
tell (init n)
tell "() "
when const (tell "const ")
tellLn $ "{ return " ++ n ++ "; }"
-- | Write's a field's setter
setter :: Field -> CppWriter
setter (Field t n) = do
tell " void set"
tell $ init (capitalize n)
tell $ '(' : ccType t
tell "value) { "
tell n
tellLn " = value; }"
-- | Writes the accessor methods. Primitives do not need non-const getters
writeAccessors :: [Field] -> CppWriter
writeAccessors fds = do
forM_ fds (\fd@(Field t _) -> do
when (not (isPrim t)) (getter fd False)
getter fd True
setter fd
lnBreak
)
-- | Makes the initialization list constructor
makeCtor :: String -> [Field] -> CppWriter
makeCtor n fds = do
tell n
tell "("
mapM_ tell args
tell ") : Packet("
tell $ constantCase n
tell "), "
mapM_ tell initList
tellLn " { }"
where
args = intersperse ", " (map argify fds)
argify (Field t n) = ccType t ++ n
initList = intersperse ", " (map initF fds)
initF (Field _ f) = f ++ '(' : f ++ ")"
-- | Makes the default and initialization list constructors
writeCtors :: Packet -> CppWriter
writeCtors (Packet n fds) = do
tellLn (" " ++ n ++ "() : Packet(" ++ constantCase n ++ ") { }")
when (not (null fds)) $ tell " " >> makeCtor n fds
lnBreak
-- | Writes the class that appears in the .h file
writeClass :: Packet -> CppWriter
writeClass p@(Packet n fds) = do
tellLn ("class " ++ n ++ " : public Packet {")
tellLn "private:"
forM_ fds (\fd -> do
tell " "
tell (show fd)
tellLn ";")
lnBreak
tellLn "public:"
writeCtors p
writeAccessors fds
tellLn " unsigned int SerializedSize() const;"
tellLn " void Serialize(ByteBuffer &buffer) const;"
tellLn " void Deserialize(ByteBuffer &buffer);"
tellLn "};\n"
-- | Writes the header file for a group of packets
writePacketHeader :: String -> [Packet] -> Map String FilePath -> CppWriter
writePacketHeader hdr ps incs = do
tellLn $ autoComment (hdr ++ ".h")
tellLn ("#ifndef " ++ guard)
tellLn ("#define " ++ guard)
lnBreak
writeIncludes incs ps
tellLn "#include \"bytebuffer.h\""
tellLn "#include \"packet.h\"\n"
mapM_ writeClass ps
tell ("#endif // " ++ guard)
where
guard = "__" ++ constantCase hdr ++ "_HEADER__"
-- | The constant Packet class
packetClass :: [String]
packetClass = [
"class Packet {",
"protected:",
" PacketType type;",
" unsigned long seqNum;\n",
"public:",
" Packet(PacketType t) : type(t) { }",
" virtual ~Packet() { }\n",
"public:",
" PacketType GetPacketType() const { return type; }",
" std::string TypeString() const;",
" unsigned long SeqNum() const { return seqNum; }",
" void SetSeqNum(unsigned long s) { seqNum = s; }\n",
" virtual unsigned int SerializedSize() const {",
" return sizeof(PacketType) + sizeof(unsigned long);",
" }",
" virtual void Serialize(ByteBuffer &b) const {",
" b.SerializeInt(static_cast<int>(type));",
" b.SerializeLong(seqNum);",
" }",
" virtual void Deserialize(ByteBuffer &b) { seqNum = b.DeserializeLong(); }\n",
" // Returned packet must be deleted",
" static Packet *DeserializePacket(ByteBuffer &b);",
"};\n"
]
-- | Write a deserialize case for a packet
writePacketCase :: Bool -> Packet -> CppWriter
writePacketCase b (Packet n _) = do
if b then tell " if (type == " else tell " else if (type == "
tell (constantCase n)
tellLn ") {"
tellLn (" " ++ n ++ " *packet = new " ++ n ++ "();")
tellLn " packet->type = type;"
tellLn " packet->Deserialize(b);"
tellLn " return packet;"
tellLn " }"
-- | Writes the packet.cpp file
writePacketImpl :: [String] -> [Packet] -> CppWriter
writePacketImpl fnames ps = do
tellLn (autoComment "packet.cpp")
tellLn "#include <string>"
tellLn "#include \"packet.h\""
tellLn "#include \"bytebuffer.h\""
forM_ fnames (\fn -> tellLn ("#include \"" ++ fn ++ ".h\""))
tellLn "\nstd::string Packet::TypeString() const {"
tellLn " switch(type) {"
forM_ ps (\(Packet n _) -> do
tellLn (" case " ++ constantCase n ++ ":")
tellLn (" return \"" ++ constantCase n ++ "\";"))
tellLn " default:"
tellLn " return \"<invalid>\";"
tellLn " }"
tellLn "}\n"
tellLn "Packet *Packet::DeserializePacket(ByteBuffer &b) {"
tellLn " PacketType type = static_cast<PacketType>(b.DeserializeInt());"
case ps of
[] -> return ()
(p:ps) -> do
writePacketCase True p
forM_ ps (writePacketCase False)
tellLn " return NULL;"
tellLn "}"
-- | Writes the main header file, including the Packet class and enum of
-- packet types
writeMainHeader :: [Packet] -> CppWriter
writeMainHeader ps = do
tellLn (autoComment "packet.h")
tellLn "#ifndef __PACKET_HEADER__"
tellLn "#define __PACKET_HEADER__\n"
tellLn "#include <string>"
tellLn "#include \"bytebuffer.h\"\n"
tellLn "enum PacketType {"
forM_ ps (\p -> do
tell " "
tell $ constantCase (packetName p)
tellLn ",")
tellLn "};\n"
mapM_ tellLn packetClass
tell "#endif // __PACKET_HEADER__"
{---------- Serialization Methods (.cpp) ----------}
-- | The code to find the size of a serialized pair
serSizePair :: CType -> CType -> String -> String -> String
serSizePair (Prim t1) (Prim t2) _ ws =
ws ++ "__size += sizeof(" ++ t1 ++ ") + sizeof(" ++ t2 ++ ");"
serSizePair (Prim t1) t2 n ws =
ws ++ "__size += sizeof(" ++ t1 ++ ");\n" ++
ws ++ ccType t2 ++ tmp2 ++ " = " ++ n ++ ".second;\n" ++
serializedSize (Field t2 tmp2) ws
where tmp2 = "tmp2_" ++ n
serSizePair t1 (Prim t2) n ws =
ws ++ ccType t1 ++ tmp1 ++ " = " ++ n ++ ".first;\n" ++
serializedSize (Field t1 tmp1) ws ++ "\n" ++
ws ++ "__size += sizeof(" ++ t2 ++ ");"
where tmp1 = "tmp1_" ++ n
serSizePair t1 t2 n ws =
ws ++ ccType t1 ++ tmp1 ++ " = " ++ n ++ ".first;\n" ++
serializedSize (Field t1 tmp1) ws ++ "\n" ++
ws ++ ccType t2 ++ tmp2 ++ " = " ++ n ++ ".second;\n" ++
serializedSize (Field t2 tmp2) ws
where
tmp1 = "tmp1_" ++ n
tmp2 = "tmp2_" ++ n
-- | The code to find the size of a serialized vector or list
serSizeList :: String -> CType -> String -> String -> String
serSizeList _ (Prim t) n ws =
ws ++ "__size += sizeof(unsigned int);\n" ++
ws ++ "__size += " ++ n ++ ".size() * sizeof(" ++ t ++ ");"
serSizeList c t n ws =
ws ++ "__size += sizeof(unsigned int);\n" ++
ws ++ "for (std::" ++ c ++ "<" ++ show t ++ wsGeneric t ++
">::const_iterator " ++ it ++ " = " ++ n ++ ".begin(); " ++ it ++ " != " ++
n ++ ".end(); " ++ "++" ++ it ++ ") {\n" ++
ws2 ++ ccType t ++ tmp ++ " = *" ++ it ++ ";\n" ++
serializedSize (Field t tmp) ws2 ++ "\n" ++ ws ++ "}"
where
ws2 = ' ' : ' ' : ws
it = n ++ "it"
tmp = "tmp_" ++ n
-- | The code to find the size of a serialized map
serSizeMap :: CType -> CType -> String -> String -> String
serSizeMap (Prim k) (Prim v) n ws =
ws ++ "__size += sizeof(unsigned int);\n" ++
ws ++ "__size += " ++ n ++ ".size() * sizeof(" ++ k ++ ");\n" ++
ws ++ "__size += " ++ n ++ ".size() * sizeof(" ++ v ++ ");"
serSizeMap k v n ws =
ws ++ "__size += sizeof(unsigned int);\n" ++
sizeT k ++ sizeT v ++
ws ++ "for (std::map<" ++ show k ++ ", " ++ show v ++ wsGeneric v ++
">::const_iterator " ++ it ++ " = " ++ n ++ ".begin(); " ++
it ++ " != " ++ n ++ ".end(); ++" ++ it ++ ") {\n" ++
(if isPrim k then "" else
ws2 ++ ccType k ++ tmpk ++ " = " ++ it ++ "->first;\n" ++
serializedSize (Field k tmpk) ws2 ++ "\n") ++
(if isPrim v then "" else
ws2 ++ ccType v ++ tmpv ++ " = " ++ it ++ "->second;\n" ++
serializedSize (Field v tmpv) ws2 ++ "\n") ++ ws ++"}"
where
ws2 = ' ' : ' ' : ws
it = n ++ "it"
tmpk = "tmpk_" ++ n
tmpv = "tmpv_" ++ n
sizeT (Prim t) = ws ++ "__size += " ++ n ++ ".size() * sizeof(" ++
t ++ ");\n"
sizeT _ = ""
-- | Gets the name of the de/serialization method
serMethod :: Bool -> String -> String
serMethod d s = "b." ++ m ++ concat (map capitalize (words t))
where
t = if isPrefixOf "unsigned " s then drop 9 s else s
m = if d then "Deserialize" else "Serialize"
-- | The code to serialize a pair
serPair :: CType -> CType -> String -> String -> String
serPair t1 t2 n ws =
ws ++ ccType t1 ++ tmp1 ++ " = " ++ n ++ ".first;\n" ++
ws ++ ccType t2 ++ tmp2 ++ " = " ++ n ++ ".second;\n" ++
serialize (Field t1 tmp1) ws ++ "\n" ++
serialize (Field t2 tmp2) ws
where
tmp1 = "tmp1_" ++ n
tmp2 = "tmp2_" ++ n
-- | The code to serialize a vector or list
serList :: String -> CType -> String -> String -> String
serList c t n ws =
ws ++ "b.SerializeInt(" ++ n ++ ".size());\n" ++
ws ++ "for (std::" ++ c ++ "<" ++ show t ++ wsGeneric t ++
">::const_iterator " ++ it ++ " = " ++ n ++ ".begin(); " ++ it ++ " != " ++
n ++ ".end(); ++" ++ it ++ ") {\n" ++ ws2 ++ ccType t ++ tmp ++ " = *" ++
it ++ ";\n" ++ serialize (Field t tmp) ws2 ++ "\n" ++ ws ++ "}"
where
ws2 = ' ' : ' ' : ws
it = n ++ "it"
tmp = "tmp_" ++ n
-- | The code to serialize a map
serMap :: CType -> CType -> String -> String -> String
serMap k v n ws =
ws ++ "b.SerializeInt(" ++ n ++ ".size());\n" ++
ws ++ "for (std::map<" ++ show k ++ ", " ++ show v ++ wsGeneric v ++
">::const_iterator " ++ it ++ " = " ++ n ++ ".begin(); " ++
it ++ " != " ++ n ++ ".end(); ++" ++ it ++ ") {\n" ++
ws2 ++ ccType k ++ tmpk ++ " = " ++ it ++ "->first;\n" ++
ws2 ++ ccType v ++ tmpv ++ " = " ++ it ++ "->second;\n" ++
serialize (Field k tmpk) ws2 ++ "\n" ++
serialize (Field v tmpv) ws2 ++ "\n" ++ ws ++"}"
where
ws2 = ' ' : ' ' : ws
it = n ++ "it"
tmpk = "tmpk_" ++ n
tmpv = "tmpv_" ++ n
-- | The code to deserialize a pair
deserPair :: CType -> CType -> String -> String -> String
deserPair t1 t2 n ws =
ws ++ show t1 ++ ' ' : tmp1 ++ ";\n" ++
deserialize (Field t1 tmp1) ws ++ "\n" ++
ws ++ show t2 ++ ' ' : tmp2 ++ ";\n" ++
deserialize (Field t2 tmp2) ws ++ "\n" ++
ws ++ n ++ ".first = " ++ tmp1 ++ ";\n" ++
ws ++ n ++ ".second = " ++ tmp2 ++ ";"
where
tmp1 = "tmp1_" ++ n
tmp2 = "tmp2_" ++ n
-- | The code to deserialize a vector or list
deserList :: CType -> String -> String -> String
deserList t n ws =
ws ++ "unsigned int " ++ size ++ " = b.DeserializeInt();\n" ++
ws ++ "for (unsigned int " ++ i ++ " = 0; " ++ i ++ " < " ++
size ++ "; ++" ++ i ++ ") {\n" ++
ws2 ++ show t ++ ' ' : tmp ++ ";\n" ++
deserialize (Field t tmp) ws2 ++ "\n" ++
ws2 ++ n ++ ".push_back(" ++ tmp ++ ");\n" ++ ws ++ "}"
where
ws2 = ' ' : ' ' : ws
size = "size_" ++ n
i = n ++ "i"
tmp = "tmp_" ++ n
-- | The code to deserialize a map
deserMap :: CType -> CType -> String -> String -> String
deserMap k v n ws =
ws ++ "unsigned int " ++ size ++ " = b.DeserializeInt();\n" ++
ws ++ "for (unsigned int " ++ i ++ " = 0; " ++ i ++ " < " ++
size ++ "; ++" ++ i ++ ") {\n" ++
ws2 ++ show k ++ ' ' : tmpk ++ ";\n" ++
ws2 ++ show v ++ ' ' : tmpv ++ ";\n" ++
deserialize (Field k tmpk) ws2 ++ "\n" ++
deserialize (Field v tmpv) ws2 ++ "\n" ++
ws2 ++ n ++ '[' : tmpk ++ "] = " ++ tmpv ++ ";\n" ++ ws ++ "}"
where
ws2 = ' ' : ' ' : ws
size = "size_" ++ n
i = n ++ "i"
tmpk = "tmpk_" ++ n
tmpv = "tmpv_" ++ n
-- | The code to get the size of a serialized field
serializedSize :: Field -> String -> String
serializedSize (Field CString n) ws = ws ++ "__size += " ++ n ++ ".size() + 1;"
serializedSize (Field (User _) n) ws = ws ++ "__size += " ++ n ++
".SerializedSize();"
serializedSize (Field (Prim t) _) ws = ws ++ "__size += sizeof(" ++ t ++ ");"
serializedSize (Field (Pair t1 t2) n) ws = serSizePair t1 t2 n ws
serializedSize (Field (Vector t) n) ws = serSizeList "vector" t n ws
serializedSize (Field (List t) n) ws = serSizeList "list" t n ws
serializedSize (Field (Map k v) n) ws = serSizeMap k v n ws
-- | The code to serialize a field
serialize :: Field -> String -> String
serialize (Field CString n) ws = ws ++ serMethod False "string" ++
'(' : n ++ ");"
serialize (Field (User _) n) ws = ws ++ n ++ ".Serialize(b);"
serialize (Field (Prim t) n) ws = ws ++ serMethod False t ++
'(' : n ++ ");"
serialize (Field (Pair t1 t2) n) ws = serPair t1 t2 n ws
serialize (Field (Vector t) n) ws = serList "vector" t n ws
serialize (Field (List t) n) ws = serList "list" t n ws
serialize (Field (Map k v) n) ws = serMap k v n ws
-- | The code to deserialize a field
deserialize :: Field -> String -> String
deserialize (Field CString n) ws = ws ++ n ++ " = " ++
serMethod True "string" ++ "();"
deserialize (Field (User _) n) ws = ws ++ n ++ ".Deserialize(b);"
deserialize (Field (Prim t) n) ws = ws ++ n ++ " = " ++
serMethod True t ++ "();"
deserialize (Field (Pair t1 t2) n) ws = deserPair t1 t2 n ws
deserialize (Field (Vector t) n) ws = deserList t n ws
deserialize (Field (List t) n) ws = deserList t n ws
deserialize (Field (Map k v) n) ws = deserMap k v n ws
-- | The code to get the size of a serialized packet
writeSerializedSize :: Packet -> CppWriter
writeSerializedSize (Packet n fds) = do
tellLn ("unsigned int " ++ n ++ "::SerializedSize() const {")
tellLn " unsigned int __size = Packet::SerializedSize();"
forM_ fds (\fd -> tellLn (serializedSize fd " "))
tellLn " return __size;"
tellLn "}\n"
-- | The code to serialize a packet
writeSerialize :: Packet -> CppWriter
writeSerialize (Packet n fds) = do
tellLn ("void " ++ n ++ "::Serialize(ByteBuffer &b) const {")
tellLn " Packet::Serialize(b);"
forM_ fds (\fd -> tellLn (serialize fd " "))
tellLn "}\n"
-- | The code to deserialize a packet
writeDeserialize :: Packet -> CppWriter
writeDeserialize (Packet n fds) = do
tellLn ("void " ++ n ++ "::Deserialize(ByteBuffer &b) {")
tellLn " Packet::Deserialize(b);"
forM_ fds (\fd -> tellLn (deserialize fd " "))
tellLn "}\n"
-- | The code to write the .cpp file for a group of packets
writeCppFile :: String -> [Packet] -> Map String FilePath -> CppWriter
writeCppFile name ps incs = do
tellLn $ autoComment (name ++ ".cpp")
writeIncludes incs ps
tellLn "#include \"bytebuffer.h\""
tellLn "#include \"packet.h\""
tellLn ("#include \"" ++ name ++ ".h\"\n")
forM_ ps (\p -> do
writeSerializedSize p
writeSerialize p
writeDeserialize p
)
{---------- Main IO ----------}
-- | Prints usage instructions
printUsage :: IO ()
printUsage = do
putStrLn "Nick's Packet Compiler"
putStrLn "Auto-generates C++ code for packet classes given fields. There are"
putStrLn "two possible invocations:\n"
putStrLn "npc [--inc=FILE] packet1 [packet2] ..."
putStrLn "npc [--inc=FILE] --all\n"
putStrLn "The first invocation will create .h and .cpp files for each packet"
putStrLn "file specified."
putStrLn "The second invocation is equivalent to invoking 'npc' with"
putStrLn "every .packet file in the current directory."
putStrLn "The inc option is used to specify the file of inclusions for"
putStrLn "user-defined classes."
data Flag =
All
| Includes (Maybe String)
deriving (Eq, Show)
getInc' :: Flag -> Maybe String
getInc' All = Nothing
getInc' (Includes s) = s
getInc :: [Flag] -> String
getInc flgs = case mapMaybe getInc' flgs of
[] -> ""
(f:_) -> f
options :: [OptDescr Flag]
options = [
Option ['a'] ["all"] (NoArg All) "compile all packets in directory",
Option ['i'] ["inc"] (OptArg Includes "FILE") "mappings of classes to header files"
]
-- | Gets the files whose extension is .packet
getFiles :: IO [FilePath]
getFiles = do
cd <- getCurrentDirectory
files <- getDirectoryContents cd
return (filter (\f -> takeExtension f == ".packet") files)
-- | Writes all the packet files
npc' :: [FilePath] -> [[Packet]] -> Map String FilePath -> IO ()
npc' packetFiles packets incs = do
forM_ (zip packetFiles packets)
(\(name,ps) -> do
base <- return (dropExtension name)
writeFile (base ++ ".h") $
execWriter (writePacketHeader base ps incs)
putStrLn ("Created " ++ base ++ ".h")
writeFile (base ++ ".cpp") $ execWriter (writeCppFile base ps incs)
putStrLn ("Created " ++ base ++ ".cpp")
)
-- | Generates all the files
npc :: [FilePath] -> [Flag] -> IO ()
npc packetFiles flgs = do
results <- sequence (map parsePackets packetFiles)
case sequence results of
Left err -> print err
-- packets :: [[Packet]]
Right packets -> do
writeFile "packet.h" $ execWriter (writeMainHeader (concat packets))
putStrLn "Created packet.h"
writeFile "packet.cpp" $
execWriter (writePacketImpl (map dropExtension packetFiles)
(concat packets))
putStrLn "Created packet.cpp"
-- Parse the inclusions file
incFile <- return (getInc flgs)
incs' <- if null incFile then (return (Right M.empty))
else parseIncludes incFile
case incs' of
Left err -> print err
Right incs -> npc' packetFiles packets incs
-- | Reads the input and generates the packet files
main :: IO ()
main = do
argv <- getArgs
when (null argv) (printUsage >> exitSuccess)
case getOpt Permute options argv of
(flgs,fs,[]) -> do
packetFiles <- if elem All flgs then getFiles else return fs
when (null packetFiles) (printUsage >> exitFailure)
npc packetFiles flgs
(_,_,errs) -> ioError (userError (concat errs ++ usageInfo info options))
where info = "Usage: npc [OPTION...] files..."