2026-11-28 22:22:16 +00:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading ;
using Steamworks ;
// Updates an existing RimWorld Steam Workshop item through the running, logged-in Steam client.
// No password: SteamAPI attaches to the client as RimWorld (app 294100), like the game does.
// Without --publish it only reads the item and checks that this Steam account owns it.
internal static class Program
{
private const uint RimWorldAppId = 294100 ;
private const int MaxTextBytes = 8000 ; // Steam's limit for a description (k_cchPublishedDocumentDescriptionMax)
private static int Main ( string [ ] args )
{
Options o ;
try { o = Options . Parse ( args ) ; }
catch ( ArgumentException e )
{
Console . Error . WriteLine ( e . Message ) ;
Console . Error . WriteLine ( Options . Usage ) ;
return 64 ;
}
if ( o . Content ! = null & & ! CheckContent ( o . Content ) ) return 65 ;
string note = o . NoteFile ! = null ? File . ReadAllText ( o . NoteFile , Encoding . UTF8 ) . Trim ( ) : null ;
string description = o . DescriptionFile ! = null ? File . ReadAllText ( o . DescriptionFile , Encoding . UTF8 ) . Trim ( ) : null ;
if ( description ! = null & & Encoding . UTF8 . GetByteCount ( description ) > MaxTextBytes )
{
Console . Error . WriteLine ( $"The description is over Steam's {MaxTextBytes}-byte limit." ) ;
return 65 ;
}
// SteamAPI.Init looks for steam_appid.txt in the working directory; the env var covers other callers.
Directory . SetCurrentDirectory ( AppContext . BaseDirectory ) ;
Environment . SetEnvironmentVariable ( "SteamAppId" , RimWorldAppId . ToString ( ) ) ;
if ( ! Packsize . Test ( ) | | ! DllCheck . Test ( ) )
{
Console . Error . WriteLine ( "Steamworks.NET and steam_api64.dll don't match. Rebuild the tool against the current RimWorld install." ) ;
return 2 ;
}
// Never call SteamAPI.RestartAppIfNecessary here: it would make Steam launch RimWorld.
if ( ! SteamAPI . Init ( ) )
{
Console . Error . WriteLine ( "SteamAPI.Init failed. Is Steam running and logged in, under the same Windows user, not as administrator?" ) ;
return 2 ;
}
try
{
return Run ( o , note , description ) ;
}
finally
{
SteamAPI . Shutdown ( ) ;
}
}
private static int Run ( Options o , string note , string description )
{
var item = new PublishedFileId_t ( o . Item ) ;
Console . WriteLine ( $"Steam account: {SteamFriends.GetPersonaName()}" ) ;
2026-11-29 10:37:57 +00:00
if ( ! QueryDetails ( item , o . Timeout , out SteamUGCDetails_t d , out string [ ] descriptors ) ) return 3 ;
2026-11-28 22:22:16 +00:00
Console . WriteLine ( $"Item {o.Item}: \" { d . m_rgchTitle } \ "" ) ;
Console . WriteLine ( $" updated {DateTimeOffset.FromUnixTimeSeconds(d.m_rtimeUpdated).ToLocalTime():yyyy-MM-dd HH:mm}" ) ;
Console . WriteLine ( $" tags {d.m_rgchTags}" ) ;
Console . WriteLine ( $" size {d.m_nFileSize / 1024.0:N0} KB" ) ;
Console . WriteLine ( $" visible {d.m_eVisibility.ToString().Replace(" k_ERemoteStoragePublishedFileVisibility ", " ")}" ) ;
if ( d . m_bBanned ) Console . WriteLine ( " BANNED on Steam" ) ;
2026-11-29 10:37:57 +00:00
// Mature-content descriptors hide the item page from logged-out visitors and from players who didn't opt in.
Console . WriteLine ( $" content {(descriptors.Length > 0 ? string.Join(" , ", descriptors) : " no mature - content descriptors ")}" ) ;
2026-11-28 22:22:16 +00:00
if ( d . m_nConsumerAppID . m_AppId ! = RimWorldAppId )
{
Console . Error . WriteLine ( $"The item belongs to app {d.m_nConsumerAppID.m_AppId}, not RimWorld." ) ;
return 3 ;
}
if ( d . m_ulSteamIDOwner ! = SteamUser . GetSteamID ( ) . m_SteamID )
{
Console . Error . WriteLine ( "This Steam account doesn't own the item." ) ;
return 3 ;
}
Console . WriteLine ( " owner this account" ) ;
if ( ! o . Publish )
{
Console . WriteLine ( ) ;
Console . WriteLine ( "Dry run: nothing was uploaded." ) ;
if ( o . Content ! = null ) Console . WriteLine ( $" would upload {o.Content}" ) ;
if ( o . Tags ! = null ) Console . WriteLine ( $" would tag {string.Join(" , ", o.Tags)}" ) ;
if ( description ! = null ) Console . WriteLine ( $" would replace the description ({Encoding.UTF8.GetByteCount(description)} bytes)" ) ;
if ( note ! = null ) Console . WriteLine ( " change note:\n" + Indent ( note ) ) ;
return 0 ;
}
if ( o . Content = = null | | note = = null )
{
Console . Error . WriteLine ( "--publish needs --content and --note-file." ) ;
return 64 ;
}
return Submit ( item , o , note , description ) ;
}
private static int Submit ( PublishedFileId_t item , Options o , string note , string description )
{
UGCUpdateHandle_t h = SteamUGC . StartItemUpdate ( new AppId_t ( RimWorldAppId ) , item ) ;
if ( ! SteamUGC . SetItemContent ( h , o . Content ) ) return Fail ( "SetItemContent refused the content folder." ) ;
if ( o . Tags ! = null & & ! SteamUGC . SetItemTags ( h , o . Tags ) ) return Fail ( "SetItemTags refused the tags." ) ;
if ( description ! = null & & ! SteamUGC . SetItemDescription ( h , description ) ) return Fail ( "SetItemDescription refused the text." ) ;
bool got = false , ioFailure = false ;
SubmitItemUpdateResult_t result = default ;
var call = CallResult < SubmitItemUpdateResult_t > . Create ( ( r , io ) = > { result = r ; ioFailure = io ; got = true ; } ) ;
call . Set ( SteamUGC . SubmitItemUpdate ( h , note ) ) ;
Console . WriteLine ( "Uploading..." ) ;
EItemUpdateStatus last = EItemUpdateStatus . k_EItemUpdateStatusInvalid ;
bool finished = Pump ( ( ) = > got , o . Timeout , ( ) = >
{
EItemUpdateStatus s = SteamUGC . GetItemUpdateProgress ( h , out ulong done , out ulong total ) ;
if ( s ! = last & & s ! = EItemUpdateStatus . k_EItemUpdateStatusInvalid )
{
Console . WriteLine ( $" {s.ToString().Replace(" k_EItemUpdateStatus ", " ")}" + ( total > 0 ? $" {done / 1024}/{total / 1024} KB" : "" ) ) ;
last = s ;
}
} ) ;
GC . KeepAlive ( call ) ;
if ( ! finished )
{
Console . Error . WriteLine ( "No answer from Steam in time. Steam may still finish the upload; check the item page before retrying." ) ;
return 5 ;
}
if ( ioFailure ) return Fail ( "Steam reported an IO failure." ) ;
if ( result . m_eResult ! = EResult . k_EResultOK ) return Fail ( $"Steam refused the update: {result.m_eResult}. {Hint(result.m_eResult)}" ) ;
Console . WriteLine ( $"Done: https://steamcommunity.com/sharedfiles/filedetails/?id={o.Item}" ) ;
if ( result . m_bUserNeedsToAcceptWorkshopLegalAgreement )
Console . WriteLine ( "Steam hides the item until you accept the Workshop legal agreement: https://steamcommunity.com/sharedfiles/workshoplegalagreement" ) ;
return 0 ;
}
2026-11-29 10:37:57 +00:00
private static bool QueryDetails ( PublishedFileId_t item , TimeSpan timeout , out SteamUGCDetails_t details , out string [ ] descriptors )
2026-11-28 22:22:16 +00:00
{
details = default ;
2026-11-29 10:37:57 +00:00
descriptors = Array . Empty < string > ( ) ;
2026-11-28 22:22:16 +00:00
UGCQueryHandle_t q = SteamUGC . CreateQueryUGCDetailsRequest ( new [ ] { item } , 1 ) ;
bool got = false , ioFailure = false ;
SteamUGCQueryCompleted_t done = default ;
var call = CallResult < SteamUGCQueryCompleted_t > . Create ( ( r , io ) = > { done = r ; ioFailure = io ; got = true ; } ) ;
call . Set ( SteamUGC . SendQueryUGCRequest ( q ) ) ;
try
{
if ( ! Pump ( ( ) = > got , timeout ) ) { Console . Error . WriteLine ( "No answer from Steam for the item query." ) ; return false ; }
if ( ioFailure | | done . m_eResult ! = EResult . k_EResultOK | | done . m_unNumResultsReturned < 1 )
{
Console . Error . WriteLine ( $"The item query failed: {(ioFailure ? " IO failure " : done.m_eResult.ToString())}." ) ;
return false ;
}
if ( ! SteamUGC . GetQueryUGCResult ( q , 0 , out details ) | | details . m_eResult ! = EResult . k_EResultOK )
{
Console . Error . WriteLine ( $"Steam has no item {item}: {details.m_eResult}." ) ;
return false ;
}
2026-11-29 10:37:57 +00:00
var ids = new EUGCContentDescriptorID [ 8 ] ;
uint count = SteamUGC . GetQueryUGCContentDescriptors ( q , 0 , ids , ( uint ) ids . Length ) ;
descriptors = ids . Take ( ( int ) Math . Min ( count , ( uint ) ids . Length ) )
. Select ( id = > id . ToString ( ) . Replace ( "k_EUGCContentDescriptor_" , "" ) ) . ToArray ( ) ;
2026-11-28 22:22:16 +00:00
return true ;
}
finally
{
GC . KeepAlive ( call ) ;
SteamUGC . ReleaseQueryUGCRequest ( q ) ;
}
}
private static bool Pump ( Func < bool > done , TimeSpan timeout , Action eachTick = null )
{
var sw = Stopwatch . StartNew ( ) ;
while ( ! done ( ) )
{
SteamAPI . RunCallbacks ( ) ;
eachTick ? . Invoke ( ) ;
if ( sw . Elapsed > timeout ) return false ;
Thread . Sleep ( 100 ) ;
}
return true ;
}
private static bool CheckContent ( string dir )
{
if ( ! Directory . Exists ( dir ) ) { Console . Error . WriteLine ( $"No content folder at {dir}." ) ; return false ; }
if ( ! File . Exists ( Path . Combine ( dir , "About" , "About.xml" ) ) ) { Console . Error . WriteLine ( $"{dir} has no About\\About.xml." ) ; return false ; }
string [ ] devOnly = { ".git" , "Source" , "Tests" , "obj" , "bin" } ;
var found = Directory . GetDirectories ( dir , "*" , SearchOption . AllDirectories )
. Where ( p = > devOnly . Contains ( Path . GetFileName ( p ) , StringComparer . OrdinalIgnoreCase ) ) . ToList ( ) ;
if ( found . Count > 0 )
{
Console . Error . WriteLine ( "The content folder holds development folders that players don't need:\n" + Indent ( string . Join ( "\n" , found ) ) ) ;
return false ;
}
var files = Directory . GetFiles ( dir , "*" , SearchOption . AllDirectories ) ;
Console . WriteLine ( $"Content: {dir} ({files.Length} files, {files.Sum(f => new FileInfo(f).Length) / 1024.0:N0} KB)" ) ;
return true ;
}
private static string Hint ( EResult r ) = > r switch
{
EResult . k_EResultAccessDenied = > "The account needs a RimWorld license." ,
EResult . k_EResultFileNotFound = > "Steam couldn't read the content folder or the preview." ,
EResult . k_EResultLimitExceeded = > "The preview is over 1 MB, or the Steam Cloud quota is full." ,
EResult . k_EResultLockingFailed = > "Steam couldn't lock the item; try again in a minute." ,
EResult . k_EResultInvalidParam = > "Steam rejected a field; check the tags and the content path." ,
_ = > "" ,
} ;
private static int Fail ( string message )
{
Console . Error . WriteLine ( message ) ;
return 4 ;
}
private static string Indent ( string text ) = > string . Join ( "\n" , text . Split ( '\n' ) . Select ( l = > " " + l . TrimEnd ( '\r' ) ) ) ;
private sealed class Options
{
public const string Usage =
"Usage: WorkshopUpload --item <publishedFileId> [--content <dir>] [--note-file <file>] [--tags Mod,1.7]\n" +
" [--description-file <file>] [--timeout <seconds>] [--publish]\n" +
"Without --publish it only reads the item and checks ownership." ;
public ulong Item ;
public string Content , NoteFile , DescriptionFile ;
public List < string > Tags ;
public TimeSpan Timeout = TimeSpan . FromMinutes ( 15 ) ;
public bool Publish ;
public static Options Parse ( string [ ] args )
{
var o = new Options ( ) ;
for ( int i = 0 ; i < args . Length ; i + + )
{
string Next ( ) = > i + 1 < args . Length ? args [ + + i ] : throw new ArgumentException ( $"{args[i]} needs a value." ) ;
switch ( args [ i ] )
{
case "--item" :
if ( ! ulong . TryParse ( Next ( ) , out o . Item ) ) throw new ArgumentException ( "--item must be a number." ) ;
break ;
case "--content" : o . Content = Path . GetFullPath ( Next ( ) ) ; break ;
case "--note-file" : o . NoteFile = ExistingFile ( Next ( ) ) ; break ;
case "--description-file" : o . DescriptionFile = ExistingFile ( Next ( ) ) ; break ;
case "--tags" : o . Tags = Next ( ) . Split ( ',' ) . Select ( t = > t . Trim ( ) ) . Where ( t = > t . Length > 0 ) . ToList ( ) ; break ;
case "--timeout" : o . Timeout = TimeSpan . FromSeconds ( int . Parse ( Next ( ) ) ) ; break ;
case "--publish" : o . Publish = true ; break ;
default : throw new ArgumentException ( $"Unknown argument {args[i]}." ) ;
}
}
if ( o . Item = = 0 ) throw new ArgumentException ( "--item is required." ) ;
return o ;
}
private static string ExistingFile ( string path )
{
string full = Path . GetFullPath ( path ) ;
if ( ! File . Exists ( full ) ) throw new ArgumentException ( $"No file at {full}." ) ;
return full ;
}
}
}