Advertisement
konalisp

xml1.fs

Oct 21st, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.41 KB | None | 0 0
  1. namespace DM //Digital Manbaby
  2.  
  3. open System
  4.  
  5. [<CLIMutable>]
  6. type GlobalState = {
  7.     manbabyName : string;
  8.     happiness : int;
  9.     GBP : int;
  10.     playerName : string;
  11.     inventory : string array;
  12. }
  13.  
  14. module FileIO =
  15.    
  16.     open System.Xml.Serialization
  17.     open System.IO
  18.    
  19.     let filename = "save.dm"
  20.    
  21.     let chkfile () =
  22.         if not (File.Exists(filename)) then
  23.             File.Create(filename) |> ignore
  24.    
  25.     type Write = GlobalState -> unit
  26.     let write : Write = fun gs ->
  27.         chkfile ()
  28.         use file = new FileStream(filename, FileMode.Create, FileAccess.Write)
  29.         let ser = new XmlSerializer(typeof<GlobalState>)
  30.         ser.Serialize(file, gs)
  31.         ()
  32.    
  33.     type Read = unit -> GlobalState
  34.     let read : Read = fun () ->
  35.         chkfile ()
  36.         use file = new FileStream(filename, FileMode.Open, FileAccess.Read)
  37.         let ser = new XmlSerializer(typeof<GlobalState>)
  38.         ser.Deserialize(file) :?> GlobalState
  39.  
  40. module Main =
  41.     [<EntryPoint>]
  42.     let main args =
  43.        
  44.         let gs : GlobalState = {
  45.             manbabyName = "Hue";
  46.             happiness = 100;
  47.             GBP = 5;
  48.             playerName = "sad lad";
  49.             inventory = [|"tendies"; "mcnuggies"|];
  50.         }
  51.        
  52.         FileIO.write gs
  53.        
  54.         let gs2 = FileIO.read ()
  55.        
  56.         printfn "%s" gs2.inventory.[0]
  57.        
  58.         0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement