Client LuaCsForBarotrauma
LuaCsSteam.cs
1 using Steamworks;
2 using Steamworks.Data;
3 using Barotrauma.Steam;
4 using System.Threading;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.IO;
8 using System.Threading.Tasks;
9 using System;
10 using Steamworks.Ugc;
11 
12 namespace Barotrauma
13 {
14  partial class LuaCsSteam
15  {
16  private struct WorkshopItemDownload
17  {
18  public Steamworks.Ugc.Item Item;
19  public string Destination;
20  public LuaCsAction Callback;
21  }
22 
23  double lastTimeChecked = 0;
24  List<WorkshopItemDownload> itemsBeingDownloaded = new List<WorkshopItemDownload>();
25 
26  public LuaCsSteam()
27  {
28 
29  }
30 
31  private static void CopyFolder(string sourceDirName, string destDirName, bool copySubDirs, bool overwriteExisting = false)
32  {
33  // Get the subdirectories for the specified directory.
34  DirectoryInfo dir = new DirectoryInfo(sourceDirName);
35 
36  if (!dir.Exists)
37  {
38  throw new System.IO.DirectoryNotFoundException(
39  "Source directory does not exist or could not be found: "
40  + sourceDirName);
41  }
42 
43  IEnumerable<DirectoryInfo> dirs = dir.GetDirectories();
44  // If the destination directory doesn't exist, create it.
45  if (!Directory.Exists(destDirName))
46  {
47  Directory.CreateDirectory(destDirName);
48  }
49 
50  // Get the files in the directory and copy them to the new location.
51  IEnumerable<FileInfo> files = dir.GetFiles();
52  foreach (FileInfo file in files)
53  {
54  string tempPath = Path.Combine(destDirName, file.Name);
55  if (!overwriteExisting && File.Exists(tempPath)) { continue; }
56  file.CopyTo(tempPath, true);
57  }
58 
59  // If copying subdirectories, copy them and their contents to new location.
60  if (copySubDirs)
61  {
62  foreach (DirectoryInfo subdir in dirs)
63  {
64  string tempPath = Path.Combine(destDirName, subdir.Name);
65  CopyFolder(subdir.FullName, tempPath, copySubDirs, overwriteExisting);
66  }
67  }
68  }
69 
70  private async void DownloadWorkshopItemAsync(WorkshopItemDownload download, bool startDownload = false)
71  {
72  if (startDownload)
73  {
74  SteamManager.Workshop.NukeDownload(download.Item);
75  SteamUGC.Download(download.Item.Id, true);
76  itemsBeingDownloaded.Add(download);
77  }
78 
79  if (download.Item.IsInstalled && Directory.Exists(download.Item.Directory))
80  {
81  if (download.Callback != null)
82  {
83  download.Callback(download.Item);
84  }
85 
86  itemsBeingDownloaded.Remove(download);
87  CopyFolder(download.Item.Directory, download.Destination, true, true);
88  return;
89  }
90  }
91 
92 
93  public async void DownloadWorkshopItem(ulong id, string destination, LuaCsAction callback)
94  {
95  if (!LuaCsFile.IsPathAllowedException(destination)) { return; }
96 
97  Option<Steamworks.Ugc.Item> itemOption = await SteamManager.Workshop.GetItem(id);
98 
99  if (itemOption.TryUnwrap(out Steamworks.Ugc.Item item))
100  {
101  DownloadWorkshopItemAsync(new WorkshopItemDownload()
102  {
103  Item = item,
104  Destination = destination,
105  Callback = callback
106  }, true);
107  }
108  else
109  {
110  throw new Exception($"Tried to download invalid workshop item {id}.");
111  }
112  }
113 
114  public void DownloadWorkshopItem(Steamworks.Ugc.Item item, string destination, LuaCsAction callback)
115  {
116  DownloadWorkshopItemAsync(new WorkshopItemDownload()
117  {
118  Item = item,
119  Destination = destination,
120  Callback = callback
121  }, true);
122  }
123 
124  public async void GetWorkshopItem(UInt64 id, LuaCsAction callback)
125  {
126  Option<Steamworks.Ugc.Item> itemOption = await SteamManager.Workshop.GetItem(id);
127 
128  if (itemOption.TryUnwrap(out Steamworks.Ugc.Item item))
129  {
130  callback(item);
131  }
132  else
133  {
134  callback(null);
135  }
136  }
137 
138  public void Update()
139  {
140  if (itemsBeingDownloaded.Count > 0 && Timing.TotalTime > lastTimeChecked) // SteamUGC.OnDownloadItemResult for some reason doesn't work, so i need to do this stupid thing.
141  {
142  foreach (var item in itemsBeingDownloaded.ToArray())
143  {
144  DownloadWorkshopItemAsync(item);
145  }
146 
147  lastTimeChecked = Timing.TotalTime + 15;
148  }
149  }
150  }
151 }
static bool IsPathAllowedException(string path, bool write=true, LuaCsMessageOrigin origin=LuaCsMessageOrigin.Unknown)
Definition: LuaCsUtility.cs:84
async void DownloadWorkshopItem(ulong id, string destination, LuaCsAction callback)
Definition: LuaCsSteam.cs:93
void DownloadWorkshopItem(Steamworks.Ugc.Item item, string destination, LuaCsAction callback)
Definition: LuaCsSteam.cs:114
async void GetWorkshopItem(UInt64 id, LuaCsAction callback)
Definition: LuaCsSteam.cs:124
delegate void LuaCsAction(params object[] args)