home *** CD-ROM | disk | FTP | other *** search
/ Arcade Explosion / ae.mdf / arcexp / descent / levels / utils / dthogexe / makehog.asm < prev    next >
Assembly Source File  |  1995-04-06  |  13KB  |  462 lines

  1. ;=============================================================================
  2. ;FILE:    MAKEHOG.ASM
  3. ;
  4. ;DESC:    Makes a Descent .HOG file from .MSN file info
  5. ;
  6. ;NOTES:   Uses Spontaneous Assembly 3.0.  Can be modified to not use it
  7. ;         at all....
  8. ;
  9. ;         Built with MASM 6.1 and Spontaneous Assembly 3.0.
  10. ;         Use the /cp:1 link option for automatic memory footprint reduction
  11. ;         at load time:
  12. ;
  13. ;         ml /c makehog.asm
  14. ;         link /cp:1 starts makehog,splithog,,sas;
  15. ;
  16. ;         This source code is placed into the public domain.
  17. ;
  18. ; Version 1.0, 03/25/95:  Initial release
  19. ;=============================================================================
  20.  
  21.  
  22.  
  23. _model    equ  <small>
  24. include        MODEL.INC
  25. include        CONSOLE.INC
  26. include        FILECNTL.INC
  27. include        FILEIO.INC
  28. include        IMATH.INC
  29.  
  30. READWRITEBUFR  equ  63 * 1024
  31. SPACE          equ  " "
  32. TAB            equ  9
  33. HIGHBLANK      equ  255
  34. CARRIAGERETURN equ  13
  35. LINEFEED       equ  10
  36. COMMA          equ  ","
  37. NULL           equ  0
  38.  
  39. .codeseg    lib
  40.  
  41. .extrn    start:auto, exit:auto, exit_ok:auto
  42. .extrn    arg_count:auto
  43. .extrn    arg_next:auto
  44. .extrn    close_h:auto
  45. .extrn    clr_region:auto
  46. .extrn    console_init:auto
  47. .extrn    cput_str:auto
  48. .extrn    getfsize_h:auto
  49. .extrn    get_version:auto
  50. .extrn    goto_xy:auto
  51. .extrn    hget_str:auto
  52. .extrn    openex_h:auto
  53. .extrn    open_h:auto
  54. .extrn    read_h:auto
  55. .extrn    set_dta:auto
  56. .extrn    str_len:auto
  57. .extrn    str_pbrk:auto
  58. .extrn    str_upr:auto
  59. .extrn    word_to_asc:auto
  60. .extrn    write_h:auto
  61.  
  62. .ends
  63.  
  64.  
  65. .dataseg
  66.  
  67. .extrn    e_code:word
  68.  
  69. RDLSize        dword     0
  70. MSNHandle      word      0                   ; handle of mission file
  71. HogHandle      word      0                   ; handle of HOG file
  72. RDLHandle      word      0                   ; handle of RDL file
  73. MemSeg         word      0                   ; read/write buffer segment
  74. ScratchBuffer  byte      130 dup(0)
  75. ScratchLength  word      $ - offset ScratchBuffer - 1
  76. HogFileName    byte      15 dup(0)
  77. HogHeader      byte      "DHF"
  78. HogHdrLength   word      $ - offset HogHeader
  79. RDLBuffer      byte      13 dup(0)
  80. RDLBufrLength  word      $ - offset RDLBuffer
  81. EndChars       byte      SPACE, TAB, HIGHBLANK, CARRIAGERETURN, LINEFEED
  82.                byte      COMMA, NULL
  83. CrLf           byte      13,10,0
  84. SignOn         byte      "MAKEHOG.EXE   ver 1.0    by Bob Clarke",13,10
  85.                byte      "Read registered Descent mission (.MSN) file and"
  86.                byte      13,10,"creates a .HOG file of the same name."
  87.                byte      13,10,10,"Press a key to begin, ESC to exit.",13,10,0
  88. DTA            FBUF      <>        ; disk transfer area
  89. WorkingWith    byte      "Working with ",0
  90. CreatingMsg    byte      "Creating ",0
  91.  
  92. ; error messages
  93.  
  94. ErrorCodeMsg   byte      "Error code:      ",0
  95. NeedMSNName    byte      "Provide the name of the mission file (.MSN) "
  96.                byte      "on the command line,",13,10
  97.                byte      "as in MAKEHOG MYGAME.MSN ",13,10,0
  98. NoMSNFile      byte      "Failure opening .MSN file.",13,10,0
  99. MemAllocError  byte      "Error allocating read/write buffer.",13,10,0
  100. HogCreateError byte      "Error creating new .HOG file.",13,10,0
  101. HdrWriteError  byte      "Error writing .HOG header bytes.",13,10,0
  102. LineReadError  byte      "Error reading .MSN line.",13,10,0
  103. NoEndInSight   byte      "Can't find end of RDL file name, "
  104.                byte      "possible invalid .MSN file format.",13,10,0
  105. NoOpenRDL      byte      "Error opening RDL file: ",0
  106. HogWriteError  byte      "Error writing to .HOG file.",13,10,0
  107. RDLReadError   byte      "Error reading from RDL file.",13,10,0
  108.  
  109. .ends
  110.  
  111.  
  112. IF NOT __TINY__
  113. .stackseg
  114. .public        stack_start
  115. .label        stack_start    word
  116.         db    1024 dup(?)    ;define a 1024 byte stack
  117. .ends
  118. ENDIF
  119.  
  120.  
  121. ;=============================================================================
  122. ;FUNC:    MAIN
  123. ;
  124. ;DESC:    Main body of program.
  125. ;
  126. ;IN:      DX        segment address of PSP
  127. ;
  128. ;ASUMS:   DS,ES     @DATASEG  (same as @CODESEG in TINY model)
  129. ;         SS        @STACKSEG (same as @CODESEG in TINY model)
  130. ;=============================================================================
  131.  
  132. .codeseg
  133.  
  134. IF __TINY__
  135. assume        cs:@codeseg, ds:@dataseg, es:@dataseg, ss:@dataseg
  136. ELSE
  137. assume        cs:@codeseg, ds:@dataseg, es:@dataseg, ss:@stackseg
  138. ENDIF
  139.  
  140. .public        main
  141. .proc        main        auto
  142.  
  143.           cld
  144.           .call console_init
  145.           .call get_version        ; init for use of file sharing flags
  146.           .call clr_region
  147.           xor  ax,ax
  148.           .call goto_xy
  149.           mov  si,offset SignOn
  150.           .call cput_str
  151.           xor  ah,ah
  152.           int  16h
  153.           cmp  al,27
  154.           jne  @F
  155.           ret                      ; violate a principle of multiple returns
  156.                                    ; since we know the AllDone code will do
  157.                                    ; nothing anyway
  158.  
  159. @@:       mov  di,offset DTA
  160.           .call set_dta
  161.  
  162. ; read the command line, looking for the .MSN file name
  163.  
  164.           .call arg_count
  165.           or   cx,cx
  166.           jnz  @F
  167.           mov  si,offset NeedMSNName
  168.           .call cput_str
  169.           jmp  AllDone
  170.  
  171. @@:       mov  si,offset ScratchBuffer
  172.           .call arg_next
  173.  
  174. ; look for the .MSN file
  175.  
  176.           mov  al,O_RDONLY+O_DENYNO
  177.           .call openex_h
  178.           jnc  @F
  179.           mov  si,offset NoMSNFile
  180.           .call cput_str
  181. ErrorCodeDisplay:
  182.           call ErrorOut
  183.           jmp  AllDone
  184.  
  185. @@:       mov  [MSNHandle],bx
  186.  
  187. ; create the .HOG file by first constructing the name, then create
  188.  
  189.           mov  si,offset ScratchBuffer
  190.           mov  di,offset HogFileName
  191. HogLoop1: lodsb
  192.           stosb
  193.           cmp  al,"."
  194.           je   HogLoop1Out
  195.           or   al,al               ; shouldn't happen
  196.           jnz  HogLoop1
  197. HogLoop1Out:
  198.           mov  ax,"OH"
  199.           stosw
  200.           mov  al,"G"
  201.           stosb
  202.           xor  al,al
  203.           stosb
  204.           mov  si,offset CreatingMsg
  205.           .call cput_str
  206.           mov  si,offset HogFileName
  207.           .call str_upr
  208.           .call cput_str
  209.           mov  si,offset CrLf
  210.           .call cput_str
  211.           mov  si,offset HogFileName
  212.           mov  ax,O_RDWR+O_DENYRW+O_CREAT+O_TRUNC
  213.           .call open_h
  214.           jnc  @F
  215.           mov  si,offset HogCreateError
  216.           .call cput_str
  217.           jmp  short ErrorCodeDisplay
  218.  
  219. @@:       mov  [HogHandle],bx
  220.  
  221. ; build the .HOG file header
  222.  
  223.           mov  si,offset HogHeader
  224.           mov  cx,[HogHdrLength]
  225.           .call write_h
  226.           jae  @F
  227.           mov  si,offset HdrWriteError
  228.           .call cput_str
  229.           jmp  short ErrorCodeDisplay
  230.  
  231. ; allocate read/write buffer
  232.  
  233. @@:       mov  ah,48h
  234.           mov  bx,READWRITEBUFR shr 4
  235.           int  21h
  236.           jnc  @F
  237.           mov  [e_code],ax
  238.           mov  si,offset MemAllocError
  239.           .call cput_str
  240.           call ErrorOut
  241.           jmp  AllDone
  242.  
  243. @@:       mov  [MemSeg],ax
  244.  
  245. ; begin loop of reading MSN file lines, looking for RDL file names,
  246. ; moving RDL info to HOG file
  247.  
  248. RDLLoop:  mov  bx,[MSNHandle]
  249.           mov  si,offset ScratchBuffer
  250.           mov  cx,[ScratchLength]
  251.           .call hget_str                ; get line from .MSN file
  252.           ja   AllDone
  253.           je   LineRead
  254.           mov  si,offset LineReadError
  255.           .call cput_str
  256.           jmp  ErrorCodeDisplay
  257. LineRead: .call str_len
  258.           mov  ax,12                    ; maximum file name length
  259.           cmp  cx,ax
  260.           jb   @F
  261.           xchg cx,ax
  262. @@:       mov  al,"."                   ; look for "." in file name
  263.           mov  di,si
  264.           repne scasb
  265.           jnz  RDLLoop                  ; if not found, can't be a line
  266.                                         ; containing an .RDL name
  267.  
  268. ; mark the end of the file name
  269.  
  270.           mov  si,offset ScratchBuffer
  271.           mov  di,offset EndChars       ; list of characters that could
  272.                                         ; signal the end of a string
  273.           .call str_pbrk
  274.           jc   @F                       ; if char not found, name is
  275.                                         ; already null-terminated
  276.           mov  di,si
  277.           xor  al,al
  278.           stosb
  279.  
  280. ; display the name of the RDL file being worked
  281.  
  282. @@:       mov  si,offset WorkingWith
  283.           .call cput_str
  284.           mov  si,offset ScratchBuffer
  285.           .call cput_str
  286.           mov  si,offset CrLf
  287.           .call cput_str
  288.  
  289. ; look for the RDL file, open it
  290.  
  291.           mov  si,offset ScratchBuffer
  292.           mov  al,O_RDONLY+O_DENYNO
  293.           .call openex_h
  294.           jnc  @F
  295.           mov  si,offset NoOpenRDL
  296.           .call cput_str
  297.           mov  si,offset ScratchBuffer
  298.           .call cput_str
  299.           mov  si,offset CrLf
  300.           .call cput_str
  301.           jmp  ErrorCodeDisplay
  302.  
  303. ; RDL is open, get size and transfer info to .HOG file
  304.  
  305. @@:       mov  [RDLHandle],bx
  306.           .call getfsize_h
  307.           mov  di,offset RDLSize
  308.           stosw                         ; store low word
  309.           xchg dx,ax
  310.           stosw                         ; store high word
  311.  
  312. ; clear name buffer
  313.  
  314.           mov  di,offset RDLBuffer
  315.           push di
  316.           mov  cx,[RDLBufrLength]
  317.           xor  al,al
  318.           rep  stosb
  319.           pop  di
  320.  
  321. ; move name to buffer
  322.  
  323.           mov  si,offset ScratchBuffer
  324.           .call str_len
  325.           mov  di,offset RDLBuffer
  326.           rep  movsb
  327.  
  328. ; write name to .HOG file
  329.  
  330.           mov  cx,[RDLBufrLength]
  331.           mov  bx,[HogHandle]
  332.           mov  si,offset RDLBuffer
  333.           .call write_h
  334.           jnc  @F
  335. HWriteErr:
  336.           mov  si,offset HogWriteError
  337.           .call cput_str
  338.           jmp  ErrorCodeDisplay
  339.  
  340. ; write RDL size to .HOG file
  341.  
  342. @@:       mov  si,offset RDLSize
  343.           mov  cx,4
  344.           .call write_h
  345.           jc   HWriteErr
  346.  
  347. ; transfer RDL file info to HOG file
  348.  
  349. ReadWriteLoop:
  350.           xor  si,si
  351.           mov  cx,READWRITEBUFR
  352.           mov  bx,[RDLHandle]
  353.           push ds
  354.           mov  ds,[MemSeg]
  355.           .call read_h
  356.           pop  ds
  357.           ja   NoMoreRDL           ; CX = 0, all done
  358.           jc   ReadRDLError
  359.           xor  si,si
  360.           mov  bx,[HogHandle]
  361.           push ds
  362.           mov  ds,[MemSeg]
  363.           .call write_h
  364.           pop  ds
  365.           jc   HWriteErr
  366.           jmp  short ReadWriteLoop
  367.  
  368. NoMoreRDL:
  369.           mov  bx,[RDLHandle]
  370.           .call close_h
  371.           mov  [RDLHandle],0
  372.           jmp  RDLLoop             ; go get another RDL file
  373.  
  374. ReadRDLError:
  375.           mov  si,offset RDLReadError
  376.           .call cput_str
  377.           jmp  ErrorCodeDisplay
  378.  
  379. ; clean up and go home
  380.  
  381. AllDone:  mov  bx,[HogHandle]
  382.           or   bx,bx
  383.           jz   @F
  384.           .call close_h
  385. @@:       mov  bx,[MSNHandle]
  386.           or   bx,bx
  387.           jz   @F
  388.           .call close_h
  389. @@:       mov  bx,[RDLHandle]
  390.           or   bx,bx
  391.           jz   @F
  392.           .call close_h
  393. @@:       mov  ax,[MemSeg]
  394.           or   ax,ax
  395.           jz   @F
  396.           mov  es,ax
  397.           mov  ah,49h
  398.           int  21h
  399. @@:       ret            ;exit to DOS with ERRORLEVEL=0
  400. .endp        main
  401.  
  402.  
  403.  
  404. ;=============================================================================
  405. ;FUNC:    ErrorOut
  406. ;
  407. ;DESC:    Displays DOS error code message
  408. ;
  409. ;IN:      nothing
  410. ;
  411. ;OUT:     nothing
  412. ;
  413. ;=============================================================================
  414.  
  415. .public        ErrorOut
  416. .proc          ErrorOut     auto uses ax bx si
  417.  
  418.           mov  ax,[e_code]
  419.           mov  bl,10
  420.           mov  si,offset ErrorCodeMsg+12
  421.           .call word_to_asc
  422.           mov  si,offset ErrorCodeMsg
  423.           .call cput_str
  424.           ret
  425.  
  426. .endp          ErrorOut
  427.  
  428. .ends
  429.  
  430.  
  431. ;=============================================================================
  432. ; Stack normalization and memory management initialization labels
  433. ;
  434. ; NOTE: These declarations must remain after the declaration of the stack
  435. ; and anything in the stack segment. These labels define the end of the
  436. ; stack and the program, which is where the near and far heaps are placed
  437. ; by default. These declarations do not affect the size of the program and
  438. ; may be left here even if the stack is not normalized and the heaps are
  439. ; not used.
  440. ;=============================================================================
  441.  
  442. .public        nheap_default, fheap_default
  443. IF NOT __TINY__
  444. .stackseg
  445.   IF __SMALL__ OR __MEDIUM__
  446. .public        stack_end        ;used by START to normalize stack
  447. .label        stack_end    word    ;must be defined past entire stack
  448.   ENDIF
  449. .label        nheap_default    word    ;used by the near heap
  450. .label        fheap_default    word    ;used by the far heap
  451. .ends
  452. ELSE
  453. _BSSEND        segment    byte public 'STACK'
  454. .label        nheap_default    word    ;used by the near heap
  455. .label        fheap_default    word    ;used by the far heap
  456. _BSSEND        ends
  457. % @codeseg    group    _BSSEND
  458. ENDIF
  459.  
  460.  
  461.         end    start        ;specify START as starting address
  462.